문제

Why would

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

yield the following error?

TypeError: expected string or buffer

도움이 되었습니까?

해결책

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')

다른 팁

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top