简体   繁体   English

Python打开一个文件并正在读取它的数据,为什么python仍然可以读取它的数据,即使我删除了这个文件?

[英]Python opened one file and is reading its data, why python still can read its data even I delete this file?

My python program is to read data from a txt file and insert data into my postgre database. 我的python程序是从txt文件中读取数据并将数据插入到我的postgre数据库中。 The program has already started and keeps running. 该程序已经启动并继续运行。 Then I accidently deleted the file on the disk, but the program still keeps running and inserting data into database. 然后我意外地删除了磁盘上的文件,但程序仍然在运行并将数据插入数据库。

Is that because when python open a file, it loads the file into memory, so that even I delete the file on the disk, it doesn't affect the running program? 是因为当python打开一个文件时,它会将文件加载到内存中,这样即使我删除磁盘上的文件,它也不会影响正在运行的程序? But my file is more than 3GB, does python really load my file into memory? 但我的文件超过3GB,python真的将我的文件加载到内存中吗? I'm worrying about whether or not my data in database is correct. 我担心数据库中的数据是否正确。

Here is my code that opens the file: 这是我打开文件的代码:

f = open("/home/minjian/Documents/tweets2009-07.txt")

My operating system is: 我的操作系统是:

Linux minjian-OptiPlex-9020 3.16.0-46-generic #62~14.04.1-Ubuntu SMP Tue Aug 11 16:27:16 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

符合POSIX标准的操作系统将实际文件数据保留在磁盘上,直到所有文件句柄都关闭,即使没有任何指向数据的链接也是如此。

The path to a file is not the file, and removing that path does not "delete the file on the disk". 文件的路径不是文件,删除该路径不会“删除磁盘上的文件”。 The path by which you access the file is merely a link to the file. 您访问该文件的路径仅仅是该文件的链接。 When python opens the file via that link, it gets access to the file and subsequent removal of the link (eg, rm foo ) does not change python's access to the file. 当python通过该链接打开文件时,它可以访问该文件,随后删除链接(例如, rm foo )不会改变python对文件的访问。 The file system will not delete the file until all references are removed, including those held by running processes. 在删除所有引用之前,文件系统不会删除该文件,包括正在运行的进程所持有的引用。

The file is not loaded into memory. 该文件加载到内存中。 It's still just sitting there on the disk. 它仍然只是坐在磁盘上。

Most filesystems do not delete the file contents, but only the reference to the file. 大多数文件系统不会删除文件内容,只会删除文件的引用。 The content gets deleted when a new file requests the space of the old one or the free space is explicitly overwritten by a tool. 当新文件请求旧文件的空间或工具明确覆盖空闲空间时,内容将被删除。

Let me explain this with NTFS. 让我用NTFS解释一下。 A File is first an entry in the MFT (Master File Table). 文件首先是MFT(主文件表)中的条目。 Its like an index saying there is a file called "xyz" and can be found at address 0x87-0x95. 它就像一个索引,说有一个名为“xyz”的文件,可以在地址0x87-0x95找到。 When you delete the file, the index entry is deleted, but the contents at the adresses 0x87-0x95 is not. 删除文件时,将删除索引条目,但不会删除地址0x87-0x95中的内容。 Your programm knows theses addresses from when it opened the file, so it can still read them, even the index entry is not longer there. 你的程序从打开文件时就知道这些地址,因此它仍然可以读取它们,甚至索引条目也不再存在。

Python won't load the file into memory unless you tell it to. 除非你告诉它,否则Python不会将文件加载到内存中。

What is going on is When your python program opens the file it creates a link to it and this linking is key in linux/unix to file handling. 发生了什么当你的python程序打开文件时,它会创建一个链接,这个链接是linux / unix到文件处理的关键。

When you list a directory and see a file, you are seeing the link that directory has to the file. 列出目录并查看文件时,您将看到该目录与该文件的链接。 When you delete the file, you are actually removing the link. 删除文件时,实际上是删除了该链接。 The operating system then notes that the file has no more links so deletes it. 然后操作系统注意到该文件没有更多链接,因此删除它。 In this case when you delete the file, the python program still has a link so the file is still on disk but you can't see it in its directory because that link is gone. 在这种情况下,当您删除文件时,python程序仍然有一个链接,因此该文件仍在磁盘上,但您无法在其目录中看到它,因为该链接已消失。 When your python program closes the file handle or exits, under the hood, python is asking the OS to remove the link it has to the file and the OS notes this is the last link and the file is gone. 当你的python程序关闭文件句柄或退出时,python要求操作系统删除它对文件的链接,操作系统注意这是最后一个链接,文件消失了。

You can use the ln command to create links so a file appears in two directories. 您可以使用ln命令创建链接,以便文件出现在两个目录中。 If you do a ls -l, the number on the left after the permissions is the number of hard links. 如果你执行ls -l,权限之后左边的数字是硬链接的数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在 python 中将 append 数据写入 excel 文件而不读取其内容? - Is there a way to append data to an excel file without reading its contents, in python? 您可以从Windows中的DMP文件读取已经运行的Python脚本的变量数据吗? - Can you read variable data of an already running Python Script from its DMP file in Windows? 我可以用它的音频文件编译 python 文件,这样没人能看到它的代码吗? - Can i compile python file with its audio file so that no one can see its code? 我正在通过解析器在python中读取文件,但无法正常工作 - I am reading file via parser in python but its not working 当我的 Python 应用程序从文件管理器打开文件时,如何获取文件路径及其名称和扩展名? - How can I get the path of the file followed by its name and extension when it is opened by my Python application from the file-manager? Python无法正确读取日志文件,除非我将其内容粘贴到新的文本文件中 - Python can't read log file correctly unless I paste its content in a new text file 在Python中,如何打开文件并在一行中读取它,之后仍能关闭文件? - In Python, how can I open a file and read it on one line, and still be able to close the file afterwards? 为什么保存我用 fitz 打开的文件会改变它的大小? - why saving a file that I opened with fitz will change its size? Flask-Python:上传 csv 文件并在应用程序中使用其数据 - Flask-Python: upload a csv file and use its data in the application 如何解析XML文件并获取其数据Python - How to parse an XML file and get its data Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM