TypeError:应为传递给Python 2.4 str.endswith()的元组的字符缓冲区对象
我的目标是获取名称以TypeError:应为传递给Python 2.4 str.endswith()的元组的字符缓冲区对象,python,string-matching,python-2.4,Python,String Matching,Python 2.4,我的目标是获取名称以ext中的任何项目结尾的文件。我有以下代码: ext=["n.log",".shm","rage"] for filename1 in os.listdir(os.getcwd()): if filename1.endswith(tuple(ext)): src=os.getcwd()+'/'+filename1 irun_log=os.path.basename(src) 使用Python 2.4
ext
中的任何项目结尾的文件。我有以下代码:
ext=["n.log",".shm","rage"]
for filename1 in os.listdir(os.getcwd()):
if filename1.endswith(tuple(ext)):
src=os.getcwd()+'/'+filename1
irun_log=os.path.basename(src)
使用Python 2.4运行此代码时,会出现以下错误:
Traceback (most recent call last):
File "./compile", line 141, in ?
if filename1.endswith(tuple(ext)):
TypeError: expected a character buffer object
非常感谢您为解决此问题提供的任何帮助。在Python 2.5中,可以将字符串元组传递给
str.endswith()
to:
str.endswith(后缀[,开始[,结束])
如果字符串以指定的后缀结尾,则返回True
,否则返回False
。后缀也可以是要查找的后缀元组。使用可选开始,从该位置开始测试。使用可选结束,停止在该位置进行比较
在版本2.5中更改:接受元组作为后缀
因此,事实证明这只是python-2.4的一个错误。我切换到Python2.7和3.5,这个错误消失了,代码正常工作。