I've created a script to rename files in a folder based on certain conditions.
if len(self.toLoc.get()) == 0:
searchRev = "_R" + newRev
for filename in os.listdir(App.pdfDir):
try:
filePath, fileExtension = os.path.splitext(filename)
sep = searchesri
rest = filename.split(sep, 1)[0] + searchRev + fromLocation + fileExtension
if fileExtension == '.pdf':
shutil.move(os.path.join(App.pdfDir, filename), os.path.join(App.pdfDir, rest))
elif fileExtension == '.xlsx':
shutil.move(os.path.join(App.pdfDir, filename), os.path.join(App.pdfDir, rest))
except IOError:
print ("Errror")
I am trying to use try and except to see if the file is open before doing any renaming. As of right now, if the file is open, the program spits out the "Error" message and renames the file but keeps a copy of the original in the directory. I was hoping there was a way to check if any of the files are open before starting the renaming process? Thanks for any advice.