我试图通过在旧文件名之前添加数字来命令目录中的所有文件(例如"Oldfilename"
应该命名为"1. Oldfilename"
)。
import os
i=0
def OrderFile(x):
ListOfFile=os.listdir(x)
for file in ListOfFile:
global i
filepath=os.path.join(x,file)
file=str(i)+'. '+file
newfilepath=os.path.join(filepath,file)
i=i+1
os.rename(filepath,newfilepath)
但是我收到了一个错误:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
'D:\\baiduyundownload\\Tempo\\Active\\Learning\\Sleep.PNG' ->
'D:\\baiduyundownload\\Tempo\\Active\\Learning\\Sleep.PNG\\1.Sleep.PNG'
答案 0 :(得分:3)
filepath
已包含文件名。您想重命名x\file
而不是filepath\file
import os
i=0
def OrderFile(x):
ListOfFile=os.listdir(x)
for file in ListOfFile:
global i
filepath=os.path.join(x,file)
file=str(i)+'. '+file
newfilepath=os.path.join(x,file)
i=i+1
os.rename(filepath,newfilepath)