title: OSError: [Errno 13] Permission denied while calling os.remove()
body: <p>For a certain script (I'm using Froglogic's Squish), I need to copy certain files into a particular directory, run an application, and then delete the files once the process ends. The problem arises during the last part. When I try (for instance)</p>
  
  <pre><code>        try:
              os.remove("C:/Program Files (x86)/madeUpDirectory/madeUpSubdirectory/filler/madeUpFile.dll")
          except LookupError, err:
              test.fail("ERROR: The file could not be deleted.", str(err));
  </code></pre>
  
  <p>I get a </p>
  
  <pre><code> OSError: [Errno 13] Permission denied: 'C:/Program Files (x86)/madeUpDirectory/madeUpSubdirectory/plugins/madeUpFile.dll'
  </code></pre>
  
  <p>error message. My understanding is that this particular error usually indicates not having the correct permissions to write to the destination in question... But that seems incongruous, since earlier on in the same script, I copy the file into that directory with this bit of code:</p>
  
  <pre><code>try:
      length_of_directory = len(origin) + 1 # +1?
      for dirname, dirnames, filenames in os.walk(origin):
          for filename in filenames:
              file_path = os.path.join(dirname, filename)# [length_of_directory:] # Combine path and filename.
              file_path = file_path.replace('\\', '/') # Reformat path to use unix-style path separators.
              dstdir =  os.path.join(dstroot, os.path.dirname(file_path)[length_of_directory:])
              dstdir = dstdir.replace('\\', '/') # Reformat path to use unix-style path separators.
              print os.access(dstdir, os.W_OK)
              if not os.path.isdir(dstdir):
                  os.makedirs(dstdir) # create all directories, raise an error if it already exists
              dstdir = os.path.join(dstdir, filename)
              dstdir = dstdir.replace('\\', '/') # Reformat path to use unix-style path separators.
              if not os.path.isfile(dstdir):
                  shutil.copy2(file_path, dstdir)
                  if os.path.isfile(dstdir):
                      test.log("%s placed." % dstdir)
              else:
                  test.log("NOTE: The file %s was already in position." % dstdir)
  except LookupError, err:
      test.fail("ERROR: Something went wrong copying", str(err));
  </code></pre>
  
  <p>Thus, I think it's safe to say I have the permissions I need - especially as I don't get any errors when overwriting the files put into the exact same location by the previous run of the script.</p>
  
  <p>I've read that this error message can arise due to the file being held open by some process or other, but I'm not sure how that could be happening; At the point when this error arises, the process that's supposed to be using the files has ended.</p>
  
  <p>I'm missing something, I'm sure - but what? How should I begin to diagnose this problem?</p>