title: Python - Why Am I Seeing This Output?
body: <p>So I'm getting into Python and I'm writing a script to:</p>
  
  <ol>
  <li>Download an RPM using urllib.urlretrieve.</li>
  <li>Extract the files using rpm2cpio and cpio.</li>
  <li>Do some stuff with the files.</li>
  <li>Clean up using shutil.rmtree.</li>
  </ol>
  
  <p>Functionally it's all good it does do all of that but since I put in the clean up code I'm getting the following output:</p>
  
  <pre><code>rpm2cpio: MyRPM.rpm: No such file or directory
  cpio: premature end of archive
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>#!/usr/bin/python
  
  from contextlib import contextmanager
  import os, subprocess, shutil
  
  @contextmanager
  def cd(directory):
      startingDirectory = os.getcwd()
      os.chdir(os.path.expanduser(directory))
      try:
          yield
      finally:
          os.chdir(startingDirectory)
  
  # Extract the files from the RPM to the temp directory
  with cd("/tempdir"):
      rpm2cpio = subprocess.Popen(["rpm2cpio", "MyRPM.rpm"], stdout=subprocess.PIPE)
      cpio = subprocess.Popen(["cpio", "-idm", "--quiet"], stdin=rpm2cpio.stdout, stdout=None)
  
  # Do
  # Some
  # Things
  # Involving
  # Shenanigans
  
  # Remove the temp directory and all it's contents
  shutil.rmtree("/tempdir")
  </code></pre>
  
  <p>If you see some syntax issues with the code here (or missing imports or something) please ignore unless it actually pertains to the reason I'm getting the two messages. I tried to strip down the script to the releveant bits. All I'm looking for is an explanation as to why the above two messages are being printed.
  I'd have assumed that the script is executed from the top down but now I'm thinking I may be wrong in this case?</p>
  
  <p>EDIT: It feels like the 'rpm2cpio' and 'cpio' commands are leaving something open as long as the script is running like something I need to explicitly close...? Does that make any sense? :)</p>
  
  <p>Thanks!
  J</p>