Python 属性错误:';元组';对象没有属性';更换';括号

Python 属性错误:';元组';对象没有属性';更换';括号,python,python-3.x,string,replace,Python,Python 3.x,String,Replace,我有一个简单的代码,用于尝试通过替换某些字符来重命名名称文件,但出现以下错误: name=name.replace=(")","") AttributeError: 'tuple' object has no attribute 'replace' 代码如下: import os os.chdir("/home/ubuntu/Desktop") nfiles=os.listdir(os.getcwd()) new_files = [nfile for nfile in nfiles if n

我有一个简单的代码,用于尝试通过替换某些字符来重命名名称文件,但出现以下错误:

name=name.replace=(")","")
AttributeError: 'tuple' object has no attribute 'replace'

代码如下:

import os
os.chdir("/home/ubuntu/Desktop")
nfiles=os.listdir(os.getcwd())
new_files = [nfile for nfile in nfiles if nfile[-4:].lower()=='.txt']

for file in new_files:

    name = file
    name=name.replace=(")","")
    name=name.replace=(",","_")
    print(name)
是一种可以应用于字符串的方法,因此应该这样调用它
replace('old\u str','new\u str')
。您没有正确使用“替换”,因此请改用此选项:

name=name.replace(")","")
name=name.replace(",","_")

否,新的_文件包含扩展名为.txt的文件名。相同的内容。有什么不同吗?它不一样,看,您有
name=name.replace=(“”),“”)
,而您应该使用
name=name.replace(“),“”)
。您正在添加一个额外的
=
@SAFAK,我添加了一个到官方Python文档的链接,以便您可以看到replace的使用。