Python: file check blocked

My goal is to find out if the file is locked by another process or not, even if I do not have access to this file!

Therefore, to be more clear, let's say I open the file using python builtin open () with the wb switch (for writing). open () will raise an IOError with errno 13 (EACCES) if

  • user does not have permission to file or
  • file locked by another process

How can I identify case (2) here?

my target platform is Windows!

+6
source share
2 answers

You can use os.access to check your access permission. If access permissions are good, then this should be the second case.

+3
source

According to the docs:

 errno.EACCES Permission denied errno.EBUSY Device or resource busy 

So just do it:

 try: fp = open("file") except IOError as e: print e.errno print e 

Extract the errno code and you will install.

+3
source

All Articles