0

I have two Python processes A and B and both are trying to write to the same file. After A has reached f = open("xx.txt", "w") and before it closes the file, I hope the other process has a way to detect that xx.txt is in use.

I've tried f = open("xx.txt", "w") in process B but it didn't throw any exception. I've also tried os.access("xx.txt", os.W_OK) in B, it returns True.

I know that I can use some file lock library such as lockfile or fcntl.But I am wondering if there is any simple solution without relying on these libs for me to detect it?

EDIT: I found a possible solution:

use os.open to open the file:

open_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY)
fd = os.open( "xx.txt", open_flags )

And we can get exception if the file already exists:

open_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY)
>>> fd = os.open( "xx.txt", open_flags )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 17] File exists: 'xx.txt'
stanleyli
  • 1,427
  • 1
  • 11
  • 28
  • possible duplicate of [Check if a file is not open( not used by other process) in Python](http://stackoverflow.com/questions/11114492/check-if-a-file-is-not-open-not-used-by-other-process-in-python) – taesu Sep 02 '15 at 19:40
  • @taesu: That question is for "check if a file is not open", and my question is "check if the file is being written". They are different. And nobody has mentioned the answer of using `os.open` in that question. – stanleyli Sep 02 '15 at 20:43

2 Answers2

0

One ugly solution is to rename the file prior to opening it and renaming it back after processing. If the renaming works, it means the other process is not using it. If the renaming fails, it means the file has been renamed and so is currently in use.

A better solution is to use another method of inter-process communication (pipes, shared memory, etc.), and if persistence is needed, a simple database such as sqlite.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
-1

I found a possible solution (in my situation that file only needs to be written once):

use os.open to open the file:

open_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY)
fd = os.open( "xx.txt", open_flags )

And we can get exception if the file already exists:

open_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY)
>>> fd = os.open( "xx.txt", open_flags )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 17] File exists: 'xx.txt'
stanleyli
  • 1,427
  • 1
  • 11
  • 28