avatar

In Python, How to check if a file is occupied or not ?

In Python, How to check if a file is occupied or not ?

bounty icon
$15
Single winner
Asked  7 months ago
Viewed  0 times

Can I use access() of the os module to check whether a file is occupied or not?

  • add comment
avatar

No you can't use access(). Because access() returns the read and write attributes of the file.Here is the test.

>>> import os
>>> fn = r'D:\temp\csdn\t.py' # test file
>>> os.access(fn, os.F_OK) # does the file exist or not
True
>>> os.access(fn, os.R_OK) # Is the file readable
True
>>> os.access(fn, os.W_OK) # Is the file writable
True
>>> os.access(fn, os.X_OK) # Is the file executable
True
>>> fp = open(fn, 'a+') # Open the file as an append write
>>> os.access(fn, os.F_OK) # Of course still there
True
>>> os.access(fn, os.R_OK) # Still writable
True
>>> os.access(fn, os.W_OK) # Stil readable
True
>>> os.access(fn, os.X_OK) # Still Eececutable
True
>>> fp.close()

As you can see, os.access() returns the file read/write attributes, which has nothing to do with whether the file is occupied or not.

Then I tried to open the file with "try", if it works, it means that the file is not occupied, if it throws an exception, it means that the file is occupied. Is this really the case? Let's verify it with code.

>>> fp1 = open(fn, 'a+')
>>> fp2 = open(fn, 'a+')
>>> fp1.close()
>>> fp2.close()

The results shows that the system does not throw an exception when the same file is opened multiple times in a write fashion. Why ? Cuz The file being opened and the file being occupied are two completely different issues. By the way, when doing the above test, do not use the 'w' method, otherwise the file would be cleared.

So, how exactly should you use Python to check whether a file is occupied or not? The asnwer go back to the operating system level, that is, by relying on the win32api module.

>>> import win32file
>>> def is_used(file_name):
try:
vHandle = win32file.CreateFile(file_name, win32file.GENERIC_READ, 0, None, win32file.OPEN_EXISTING, win32file.FILE_ATTRIBUTE_NORMAL, None)
return int(vHandle) == win32file.INVALID_HANDLE_VALUE
except:
return True
finally:
try:
win32file.CloseHandle(vHandle)
except:
pass

>>> fn = r'D:\temp\csdn\t.py'
>>> is_used(fn)
False
>>> fp = open(fn, 'a+')
>>> is_used(fn)
True
>>> fp.close()
>>> is_used(fn)
False

is_used() is the answer.

  • Taken
  • add comment
  • 0
Sign In
Sign In
avatar

In Python, How to check if a file is occupied or not ?

In Python, How to check if a file is occupied or not ?

bounty icon
$15
Single winner
Asked  7 months ago
Viewed  0 times

Can I use access() of the os module to check whether a file is occupied or not?

  • add comment
avatar

No you can't use access(). Because access() returns the read and write attributes of the file.Here is the test.

>>> import os
>>> fn = r'D:\temp\csdn\t.py' # test file
>>> os.access(fn, os.F_OK) # does the file exist or not
True
>>> os.access(fn, os.R_OK) # Is the file readable
True
>>> os.access(fn, os.W_OK) # Is the file writable
True
>>> os.access(fn, os.X_OK) # Is the file executable
True
>>> fp = open(fn, 'a+') # Open the file as an append write
>>> os.access(fn, os.F_OK) # Of course still there
True
>>> os.access(fn, os.R_OK) # Still writable
True
>>> os.access(fn, os.W_OK) # Stil readable
True
>>> os.access(fn, os.X_OK) # Still Eececutable
True
>>> fp.close()

As you can see, os.access() returns the file read/write attributes, which has nothing to do with whether the file is occupied or not.

Then I tried to open the file with "try", if it works, it means that the file is not occupied, if it throws an exception, it means that the file is occupied. Is this really the case? Let's verify it with code.

>>> fp1 = open(fn, 'a+')
>>> fp2 = open(fn, 'a+')
>>> fp1.close()
>>> fp2.close()

The results shows that the system does not throw an exception when the same file is opened multiple times in a write fashion. Why ? Cuz The file being opened and the file being occupied are two completely different issues. By the way, when doing the above test, do not use the 'w' method, otherwise the file would be cleared.

So, how exactly should you use Python to check whether a file is occupied or not? The asnwer go back to the operating system level, that is, by relying on the win32api module.

>>> import win32file
>>> def is_used(file_name):
try:
vHandle = win32file.CreateFile(file_name, win32file.GENERIC_READ, 0, None, win32file.OPEN_EXISTING, win32file.FILE_ATTRIBUTE_NORMAL, None)
return int(vHandle) == win32file.INVALID_HANDLE_VALUE
except:
return True
finally:
try:
win32file.CloseHandle(vHandle)
except:
pass

>>> fn = r'D:\temp\csdn\t.py'
>>> is_used(fn)
False
>>> fp = open(fn, 'a+')
>>> is_used(fn)
True
>>> fp.close()
>>> is_used(fn)
False

is_used() is the answer.

  • Taken
  • add comment
  • 0