I would like to create a python function where I pass as arguments the name and the directory path of some files which I want to rename; something like def my_fun (name, path):
The directory path is the same for each file. I did it in this way but I cannot turn it into a function form as I look for.
path = input("tap the complete path where files are")
for name in files_names:
name = input("insert file name as name.ext")
old_name = os.path.join(path, files_names)
new_name = os.path.join(path, name)
os.rename(old_name, new_name)
If you want to be able to pass in the path and file name (a list of names) as arguments:
def rn(path, files_names):
new_name = input("Input the new name: ")
for i,name in enumerate(files_names):
old_name = os.path.join(path, name)
new_name = os.path.join(path, f"{new_name.split('.')[0]}{i}.{new_name.split('.')[1]}")
os.rename(old_name, new_name)
path = input("tap the complete path where files are")
file_names = ['image.png','car.png','image1.png','photo.png','cake.png']
rn(path, file_names)
This will rename all the files in the list with what the user input into new_name
, labeled starting from 0
.