I'm having difficulty understanding the concept of how Python reads a file when it was deleted after open
'ed. Here is the code:
>>> import os >>> os.system('cat foo.txt') Hello world! 0 >>> f <_io.TextIOWrapper name='foo.txt' mode='r' encoding='UTF-8'> >>> os.system('rm -f foo.txt') 0 >>> os.system('cat foo.txt') cat: foo.txt: No such file or directory 256 >>> f.read() 'Hello world!\n' >>>
Text and binary modes give the same result.
I tried this also for large files larger than 1 GB, and they were also read after deletion. The open
operation occurs almost instantly even for very large files.
Where does Python get data if the open file no longer exists?
I tested this test for
python 3.4.3 / 3.5.2
ubuntu 14.04 / 16.04
source share