python - Check to see if file is open before doing anything -


i've created script rename files in folder based on conditions.

    if len(self.toloc.get()) == 0:         searchrev = "_r" + newrev         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 trying use try , except see if file open before doing renaming. of right now, if file open, program spits out "error" message , renames file keeps copy of original in directory. hoping there way check if of files open before starting renaming process? advice.

edit: possible duplicate

you try open file first, throw , ioexception if is:

if len(self.toloc.get()) == 0: searchrev = "_r" + newrev filename in os.listdir(app.pdfdir):     try:         filepath, fileextension = os.path.splitext(filename)         open(os.path.join(app.pdfdir, filename),"r+") f:              pass         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") 

as assuring file not opened after check , during process, shutil.move atomic (essentially locks access while using) when on same filesystem.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -