我试图通过在旧文件名之前添加数字来排序目录中的所有文件(例如,"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'发布于 2016-03-03 03:48:26
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)https://stackoverflow.com/questions/35762741
复制相似问题