xxxxxxxxxx
import os
path_to_file = 'C:/Users/Red/test.txt' # For example the file is here
new_name = 'cool.txt' # Example name
os.rename(path_to_file, os.path.join(os.path.dirname(path_to_file), new_name))
# It will be renamed to cool.txt aka 'C:/Users/Red/cool.txt'
xxxxxxxxxx
import os
old_file_name = "/home/career_karma/raw_data.csv"
new_file_name = "/home/career_karma/old_data.csv"
os.rename(old_file_name, new_file_name)
print("File renamed!")
xxxxxxxxxx
import os
os.rename(r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt')
xxxxxxxxxx
# for multiple files
import os
for dirname in os.listdir("."):
print((dirname[:-4]).zfill(6)+'.txt')
os.rename(dirname, (dirname[:-4]).zfill(6)+'.txt')