我正在尝试使用 os.rename 一次重命名多个文件

I am trying to rename multiple files at a time using os.rename

我正在尝试在 win8.1

中使用 os.rename 重命名多个文件
import os

path = "C:\Users\Aniket\Desktop\Python projects\p"
di = os.listdir(path)
os.chdir(path)
for file in di :
    i = 0
    file_name , file_ext = os.path.splitext(file)
    new_name = "file"+str(i)+f"{file_ext}"
    os.rename(new_name, file)
    i+=1

我希望 6464.txt 重命名为 file0.txt。 但是 FileNotFoundError: system cannot find the specified file: 'file0.txt'-> '6464.txt' 出现。 (file0 是新名称,而 6464 是现有名称)

你想错了。 os.rename 作品 os.rename(src, dst)。所以只需切换 os.rename(file, new_name)