0

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

2
  • str(items.split('_', 4)) Commented Mar 31, 2020 at 4:50
  • 1
    items.split('_',4) returns a list. Hence, the error is when you try to concat a list and a str. Commented Mar 31, 2020 at 4:52

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't realise its complaining about the new list, newbie error,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.