为什么会
re.search("\.docx", os.listdir(os.getcwd()))
产生以下错误?
TypeError: expected string or buffer
最佳答案
因为 os.listdir
返回一个 list
但 re.search
想要一个字符串。
做你正在做的事情最简单的方法是:
[f for f in os.listdir(os.getcwd()) if f.endswith('.docx')]
甚至:
import glob
glob.glob('*.docx')
关于python re.search错误TypeError : expected string or buffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22896914/