Say I have a folder with n csv
files which I want to rename. The new filename is going to be something like ABxxxx
, with xxxx
being a progressive number from 1 to 1000.
While doing this, how can I retain the original file extension, which is csv
?
What I have done so far has changed the filenames but has pruned away the extension:
directory=r'C:\Me\MyDir'
subdir=[x[0] for x in os.walk(directory)]
subdir.pop(0)
for i in subdir:
temp_dir=r''+i
os.chdir(temp_dir)
a='A'
b='B'
for file in glob.glob("*.csv"):
for i in range(1,1001):
newname=a+b+i
os.rename(file,newname)
.csv
tonewname
? – Thanetnewname+".csv"
? – Pardoesext = filename[-4:]
, assign it to a variable, use that variable to construct a new filename. – Gluey