Assume that, file.txt is closed or deleted during the delay between open and write. (or it can ?)
Then, this situation can occur TOCTOU ?
with statement sure that atomic until with block or not?
with open("file.txt") as f :
# ...delayed...
f.write("something")
Can this occur?
Use case 1: Python itself deletes the file*
yes it can happen. I just tested like this:
In [1]: with open("file.txt", "w") as f :
...: f.write("Something Old")
...:
In [2]: !cat ./file.txt
Something Old
In [3]: import os
...: with open("file.txt","w") as f:
...: os.remove("./file.txt")
...: print f.write("Something new")
...:
None
In [4]: !cat ./file.txt
cat: ./file.txt: No such file or directory
Use Case 2: Other than python deletes the file.
Then also, found the behavior to be same.
In [1]: !cat ./file.txt
Something Old
In [2]: import os
...: import time
...:
...: with open("file.txt","w") as f:
...: time.sleep(15)
...: print f.write("Something new")
...:
None
In [3]: !cat ./file.txt
cat: ./file.txt: No such file or directory
How to avoid it?
You can use exclusive lock from fcntl.lockf()
Edit: There is one more caveat here. Locking the file may not be straight forward and may be OS dependent like What is the best way to open a file for exclusive access in Python?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With