我想创建一个python函数,在这个函数中,我传递的参数是 名称 和目录 路径 的一些文件,我想将其重命名,比如说像 def my_fun (name, path):
每个文件的目录路径都是一样的。我是这样做的,但我不能把它变成一个函数形式。
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)
如果你希望能够将路径和文件名传入 (名单) 作为参数。
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)
这将用用户输入的参数重命名列表中的所有文件。new_name
,标明从 0
.