3

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")
Ho0ony
  • 158
  • 11
  • 2
    `with` blocks aren’t atomic (neither in general nor with files). On Windows, IIRC, you can’t delete a file that’s being written to; elsewhere, unlinking a file doesn’t invalidate handles to it. Either way, there’s no problem with continuing to write. **tl;dr: no** – Ry- Mar 31 '17 at 05:44
  • @Ryan but there's problem when I read deleted file. right ? – Ho0ony Mar 31 '17 at 06:08
  • 2
    No. As long as the file is open, it won’t actually be deleted by conventional means. – Ry- Mar 31 '17 at 06:11
  • Oh, I'm stupid. I just try it. thank you ! – Ho0ony Mar 31 '17 at 06:16

1 Answers1

2

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?

Community
  • 1
  • 1
Gurupad Hegde
  • 2,155
  • 15
  • 30