xxxxxxxxxx
# Python program to rename all file
# names in your directory
import os
os.chdir('D:\\Geeksforgeeks')
print(os.getcwd())
for count, f in enumerate(os.listdir()):
f_name, f_ext = os.path.splitext(f)
f_name = "geek" + str(count)
new_name = f'{f_name}{f_ext}'
os.rename(f, new_name)
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')