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?