Reputation: 63
I need to change file names inside a folder
My file names are
BX-002-001.pdf
DX-001-002.pdf
GH-004-004.pdf
HJ-003-007.pdf
I need to add an additional zero after '-' at the end, like this
BX-002-0001.pdf
DX-001-0002.pdf
GH-004-0004.pdf
HJ-003-0007.pdf
I tried this
all_files = glob.glob("*.pdf")
for i in all_files:
fname = os.path.splitext(os.path.basename(i))[0]
fname = fname.replace("-00","-000")
My code is not working, can anyone help?
Upvotes: 0
Views: 222
Reputation: 2439
fname = fname.replace("-00","-000")
only changes the variable fname
in your program. It does not change the filename on your disk.
you can use os.rename()
to actully apply the changes to your files:
all_files = glob.glob("*.pdf")
for i in all_files:
fname = os.path.splitext(os.path.basename(i))[0]
fname = fname.replace("-00","-000")
os.rename(i, os.path.join(os.path.dirname(i), fname ))
Upvotes: 2