Reputation: 550
I am trying to order all the files in a directory simply by adding numbers before their old file names(e.g "Oldfilename"
should be named "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)
But I get an error:
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'
Upvotes: 1
Views: 5838
Reputation: 3612
filepath
already contains the filename in it. You want to rename x\file
not 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)
Upvotes: 4