test.txt
fp = open('test.txt', 'r')
fp.read()
我正在从文件中获取所有内容但我所知道的是,当我们创建文件对象时,该文件将无法加载到RAM中。那么如果我从硬盘上删除它,那么我从哪里删除文件内容?
答案 0 :(得分:1)
详细信息可能取决于您正在使用的操作系统,但对于您正在谈论的Linux,更合适的问题是如何将文件打开怎么可能,但会发生什么何时删除文件。您可以在unlink(2)
系统调用:
...
unlink() deletes a name from the filesystem. If that name was the last
link to a file and no processes have the file open, the file is deleted
and the space it was using is made available for reuse.
If the name was the last link to a file but any processes still have
the file open, the file will remain in existence until the last file
descriptor referring to it is closed.
...
如果你从python的角度来看,该文件可能在文件系统中不再有一个名称,但它仍然存在,你也可以执行以下操作:
>>> f = open('TEST')
>>> os.unlink('TEST')
>>> print(os.fstat(f.fileno()))
os.stat_result(st_mode=33188, st_ino=146934248, st_dev=2431, st_nlink=0, st_uid=1000, st_gid=100, st_size=5, st_atime=1517409090, st_mtime=1517409090, st_ctime=1517409124)