Reputation: 59
i have a simple code for trying to rename a name files by replace some characters but I get this error:
name=name.replace=(")","")
AttributeError: 'tuple' object has no attribute 'replace'
Code is here:
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)
Upvotes: 1
Views: 2146
Reputation: 69873
replace
is a method that you can apply to strings, so you should call it this way replace('old_str', 'new_str')
. You are not using replace properly, so use this instead:
name=name.replace(")","")
name=name.replace(",","_")
Upvotes: 1