abacusreader
abacusreader

Reputation: 595

python parsing item from a list as string

I am trying to read the contents of a folder and parsing the filenames returned using the following code, I get a type error when executing the code

templist = os.listdir(fg_image_dir)

for items in templist:
    #print(items.text)
    print('File  size is '+items.split('_',4))

 print('File  size is '+items.split('_',4)) TypeError: must be str, not list

As per the error items is not a str, so I add the line items.text just to see what error message that will produce, the error message is as follows

 print(items.text)
AttributeError: 'str' object has no attribute 'text'

Why is a string attribute not considered as string in the first case?

How do I split a filename returned by my code to look for a text pattern

Upvotes: 0

Views: 42

Answers (1)

SetMao
SetMao

Reputation: 71

your items is a str now, just use

print(items)

and items.split('_', 4) will return a list, list and can not add with str if you want to get str has split, you should do this

items.split('_',4)[index]

Upvotes: 1

Related Questions