【发布时间】:2017-03-02 02:39:40
【问题描述】:
我很难理解 Python 在文件被 open'ed 删除后如何读取文件的概念。代码如下:
>>> 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'
>>>
文本和二进制模式给出相同的结果。
我也尝试过对大小超过 1Gb 的大文件进行此操作,并且它们在被删除后也会被读取。即使对于非常大的文件,open 的操作也几乎是瞬间发生的。
如果打开的文件不再存在,Python 从哪里获取数据?
我运行了这个测试
python 3.4.3 / 3.5.2ubuntu 14.04 / 16.04
【问题讨论】:
标签: python linux file readfile