What will happen if I delete a input file while some program is reading data from that file?
<p>If there is a python script doing this :</p>

<pre><code>with open('large_input_file.log', 'rb') as f :
    for each_line in f :
        do something .....
</code></pre>

<p>Let's call this script <code>a.py</code></p>

<p><code>large_input_file.log</code> is about 16GB. <code>a.py</code> will take hours to process this file.</p>

<p>What will happen if I do this (under Linux):</p>

<ol>
<li><p>keep <code>a.py</code> running</p></li>
<li><p>delete <code>large_input_file.log</code></p></li>
<li><p>replace <code>large_input_file.log</code> with different content but same name</p></li>
</ol>

<p>Is <code>a.py</code> able to get the correct data in <code>large_input_file.log</code> before I delete it? (I guess this is what will happen.)</p>

<p>Or will <code>a.py</code> get new data starting with the same offset in the new <code>large_input_file.log</code></p>

<p>Can you explain it in kernel level or filesystem level? (How does linux accomplish this)?</p>

<p>-----------------Below is added after some answer------------------------</p>

<p>What if my disk size is 16Gb, so there can be store only one <code>large_input_file.log</code>.</p>

<p>What will happen if I delete <code>large_input_file.log</code> and create another 16Gb <code>large_input_file.log</code> file ?</p>

<p>Let's create a file:</p>

<pre><code># echo foo &gt; test.txt
</code></pre>

<p>Now we'll use <code>tail</code> to monitor it for changes:</p>

<pre><code># tail -f test.txt
foo
</code></pre>

<p>Let's open another tab on our terminal, and check the pid of our <code>tail</code> process:</p>

<pre><code># ps aux | grep -i tail
root      5458  0.0  0.0   7484   724 ?        S    Sep15   0:13 tail -f -n 0 /var/log/syslog
root      5919  0.0  0.0   7484   784 ?        S    Sep15   0:13 tail -f -n 0 /var/log/syslog
root      6381  0.0  0.0   7484   840 ?        S    Sep15   0:14 tail -f -n 0 /var/log/syslog
emil     27789  0.0  0.0   8852   784 pts/8    S+   12:26   0:00 tail -f test.txt
emil     27826  0.0  0.0  15752  1016 pts/9    S+   12:26   0:00 grep -i tail
</code></pre>

<p>So, in my case the pid is 27789. We can look at the open files of the process by checking the <code>/proc/27789/fd</code> directory:</p>

<pre><code># ls -lah /proc/27789/fd/
total 0
dr-x------ 2 emil emil  0 Sep 20 12:26 .
dr-xr-xr-x 9 emil emil  0 Sep 20 12:26 ..
lrwx------ 1 emil emil 64 Sep 20 12:26 0 -&gt; /dev/pts/8
lrwx------ 1 emil emil 64 Sep 20 12:26 1 -&gt; /dev/pts/8
lrwx------ 1 emil emil 64 Sep 20 12:26 2 -&gt; /dev/pts/8
lr-x------ 1 emil emil 64 Sep 20 12:26 3 -&gt; /home/emil/test.txt
lr-x------ 1 emil emil 64 Sep 20 12:26 4 -&gt; anon_inode:inotify
</code></pre>

<p>Here we see that <code>tail</code> has a file descriptor called 3 to <code>test.txt</code>. What if we delete the file?</p>

<pre><code># rm test.txt
# ls -lah /proc/27789/fd
total 0
dr-x------ 2 emil emil  0 Sep 20 12:26 .
dr-xr-xr-x 9 emil emil  0 Sep 20 12:26 ..
lrwx------ 1 emil emil 64 Sep 20 12:26 0 -&gt; /dev/pts/8
lrwx------ 1 emil emil 64 Sep 20 12:26 1 -&gt; /dev/pts/8
lrwx------ 1 emil emil 64 Sep 20 12:26 2 -&gt; /dev/pts/8
lr-x------ 1 emil emil 64 Sep 20 12:26 3 -&gt; /home/emil/test.txt (deleted)
lr-x------ 1 emil emil 64 Sep 20 12:26 4 -&gt; anon_inode:inotify
</code></pre>

<p>The file descriptor still exists, but <code>ls</code> will helpfully let us know that the file has been deleted.</p>

<p>As Igor says, each file has a physical location on disk where the raw data exists. In order to find files, the system maintains a table of inodes mapping file names to actual data. Removing a file doesn't wipe the data from disk, it simply modifies the inode. The data will still exist, until it's explicitly overwritten by something else. In this specific case, though, the kernel contains extra code to make sure that the file continues to exist - and won't be overwritten - until it's no longer open by any process.</p>

python linux