0

I've been playing around a bit with file-based Streams in Python and have noticed that several methods (e.g. write, close, writable) do not raise Exceptions when an underlying file for the stream has been deleted (e.g. per a call to os.remove().

Having reviewed the standard library docs here - there isn't any content referring specifically how such methods would behave if the stream's underlying file was deleted - but the behavior I've observed seems unintuitive. One can imagine, for example, a case where an application believes it has successfully written critical information to a log file - but the log file, in fact, has been deleted and the application is unaware that is no longer exists because no Exception is raised.

Here is some example code that illustrates this behavior. I used Python 3.10.7 to execute it.

import os
from os.path import exists

p = "./myfile.txt"
f = open(p, "w")
f.write("Here is my content\n")
f.flush()
os.remove(p)
print(exists(p))  # False (as expected)
print(f.writable())  # True - but 'myfile.txt' has been deleted!
f.write("Some more content")  # Does not raise an exception!
f.flush()  # Does not raise an exception
exists(p)  # Still False - which is expected (and I confirmed there is no file at path "p")
f.close()  # Does not raise an exception!
# Line below fails with "ValueError: I/O operation on closed file."  That is consistent with the documentation for
# "close()" in the io module.
f.write("This better fail!")

As indicated above, I expected the f.write, f.flush, f.close and f.writable operations to raise Exceptions once the file was deleted with the os.remove() call. None of those operations, however, raised an Exception.

Obviously, this is very unlikely to be a bug. But if not, why is this behavior desirable and is this behavior specific to Python or file-based streams as a whole?

1 Answers1

0

reading/writing to files is not handled by python, it is handled by the operating system, python just puts the data in a buffer, and the operating system is responsible for writing it to the disk. (by calling a kernel mode function)

what the operating system does when it writes to a file that doesn't exist is up to the operating system, for example on windows the above code does raise an error

Traceback (most recent call last):
  File "example_python.py", line 8, in <module>
    os.remove(p)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: './myfile.txt'

this is not the case for linux, and you can read more about it in this answer What happens to an open file handle on Linux if the pointed file gets moved or deleted

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23