title: Python: Closing and removing files
body: <p>I'm trying to unzip a file, and read one of the extracted files, and delete the extracted files.</p>
  
  <ol>
  <li>Files extracted (e.g. we got file1 and file2)</li>
  <li><p>Read file1, and close it.</p>
  
  <pre><code>with open(file1, 'r') as f:
      data = f.readline()
  f.close()
  </code></pre></li>
  <li><p>Do something with the "data".</p></li>
  <li><p>Remove the files extracted. </p>
  
  <pre><code>os.remove(file1)
  </code></pre></li>
  </ol>
  
  <p>Everything went fine, except it received these messages at the end. The files were also removed. How do I close the files properly?</p>
  
  <pre><code>    /tmp/file1: No such file or directory
      140347508795048:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('/tmp/file1','r')
      140347508795048:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  (My script looks similar to these)</p>
  
  <pre><code>#!/usr/bin/python
  import subprocess, os
  
  infile = "filename.enc"
  outfile = "filename.dec"
  opensslCmd = "openssl enc -a -d -aes-256-cbc -in %s -out %s" % (infile, outfile)   
  subprocess.Popen(opensslCmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,      close_fds=True)
  os.remove(infile)
  </code></pre>