title: How are file objects cleaned up in Python when the process is killed?
body: <p>What happens to a file object in Python when the process is terminated? Does it matter whether Python is terminated with <code>SIGTERM</code>, <code>SIGKILL</code>, <code>SIGHUP</code> (etc.) or by a <code>KeyboardInterrupt</code> exception?</p>
  
  <p>I have some logging scripts that continually acquire data and write it to a file. I don't care about doing any extra clean up, but I just want to make sure that log file is not corrupted when Python is abruptly terminated (e.g. I could leave it running in the background and just shutdown the computer). I made the following test scripts to try to see what happens:</p>
  
  <p><code>termtest.sh</code>:</p>
  
  <pre><code>for i in $(seq 1 10); do
      python termtest.py $i &amp; export pypid=$!
      sleep 0.3
      echo $pypid
      kill -SIGTERM $pypid
  done
  </code></pre>
  
  <p><code>termtest.py</code>:</p>
  
  <pre><code>import csv
  import os
  import signal
  import sys
  
  end_loop = False
  
  
  def handle_interrupt(*args):
      global end_loop
      end_loop = True
  
  
  signal.signal(signal.SIGINT, handle_interrupt)
  
  with open('test' + str(sys.argv[-1]) + '.txt', 'w') as csvfile:
      writer = csv.writer(csvfile)
      for idx in range(int(1e7)):
          writer.writerow((idx, 'a' * 60000))
          csvfile.flush()
          os.fsync(csvfile.fileno())
          if end_loop:
              break
  </code></pre>
  
  <p>I ran <code>termtest.sh</code> with different signals (changed <code>SIGTERM</code> to <code>SIGINT</code>, <code>SIGHUP</code>, and <code>SIGKILL</code> in <code>termtest.sh</code>) (note: I put an explicit handler in <code>termtest.py</code> for <code>SIGINT</code> since Python does not handle that one other than as <code>Ctrl+C</code>). In all cases, all of the output files had only complete rows (no partial writes) and did not appear corrupted. I put the <code>flush()</code> and <code>fsync()</code> calls to try to make sure the data was being written to disk as much as possible so that the script had the greatest chance of being interrupted mid-write.</p>
  
  <p>So can I conclude that Python always completes a write when it is terminated and does not leave a file in an intermediate state? Or does this depend on the operating system and file system (I was testing with Linux and an ext4 partition)?</p>