1

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.

unknownid
  • 105
  • 1
  • 8
  • That's not reliable, since a file could be opened after you check. – Barmar Apr 24 '14 at 15:40
  • Whether you can (or need to) do this depends on your operating system. Assuming POSIX semantics, it's unnecessary, as a file can have multiple names, so an open file handle does not depend on the name of a file. Multiple processes can have a file open for reading at once (or possibly even for writing, although care must be taken to coordinate writes). Renaming an open file should not affect file handles already open in other processes. – chepner Apr 24 '14 at 16:14
  • possible duplicate of [check if a file is open in Python](http://stackoverflow.com/questions/6825994/check-if-a-file-is-open-in-python) – Farmer Joe Apr 24 '14 at 17:43

1 Answers1

1

EDIT: Possible duplicate

You could simply try to open the file first, which would throw and IOException if it is:

if len(self.toLoc.get()) == 0:
searchRev = "_R" + newRev
for filename in os.listdir(App.pdfDir):
    FilePath, fileExtension = os.path.splitext(filename)
    try:
        with open(os.path.join(App.pdfDir, filename),"r+") as f:
             pass
    except IOError:
        print ("Errror")
    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))

As for assuring that the file is not opened after the check and during the process, shutil.move is atomic (essentially locks access while using) when on the same filesystem.

Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
  • 1
    I think you should put things after `pass` into an `else` block so the `try` is only monitoring the `open`, not all others. – Jason Apr 28 '19 at 04:51