python re.search错误TypeError : expected string or buffer

标签 python expression

为什么会

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

产生以下错误?

TypeError: expected string or buffer

最佳答案

因为 os.listdir 返回一个 listre.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/

相关文章:

python - 在 python (matplotlib) 中优雅地停止(而不是暂停)动画情节

python - 在 Python Pandas 中搜索数据框中的项目并将列转置为行

c# - 用于比较 string[] 是否包含给定字符串的 lambda 表达式

regex - Powershell正则表达式用可变长度的字符串替换数字

令人困惑的答案 : One says *myptr++ increments pointer first, 其他人说 *p++ 取消引用旧指针值

c++ - 如何确定表达式值的类型?

javascript - 单个字符串上的多个正则表达式

python - 如何从列表中的多个列表中创建一个表格?

python mercurial api : get incoming changes

python - 查找公共(public)列表序列