5

Why would

re.search("\.docx", os.listdir(os.getcwd()))

yield the following error?

TypeError: expected string or buffer

2 Answers 2

10

Because os.listdir returns a list but re.search wants a string.

The easiest way to do what you are doing is:

[f for f in os.listdir(os.getcwd()) if f.endswith('.docx')]

Or even:

import glob
glob.glob('*.docx')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for being gentle, I just started python today! Thank you!
2

re.search() expects str as the second argument. Refer docs to know more.

import re, os

a = re.search("\.docx", str(os.listdir(os.getcwd())))
if a:
    print(True)
else:
    print(False)

Comments

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.