import os
# Absolute path of a file
old_name = r"E:demosfiles
eportsdetails.txt"
new_name = r"E:demosfiles
eports
ew_details.txt"
# Renaming the file
os.rename(old_name, new_name)
# for multiple files
import os
for dirname in os.listdir("."):
print((dirname[:-4]).zfill(6)+'.txt')
os.rename(dirname, (dirname[:-4]).zfill(6)+'.txt')
import os
for dirname in os.listdir("."):
if os.path.isdir(dirname):
for i, filename in enumerate(os.listdir(dirname)):
os.rename(dirname + "/" + filename, dirname + "/" + str(i) + ".bmp")
>>> os.listdir()
['test']
>>> os.rename('test','new_one')
>>> os.listdir()
['new_one']
from pathlib import Path
path = Path("D: est")
temp = path.rename("Enter new folder name")
new_path = temp.absolute()