Reputation: 133
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)
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));
I get a
OSError: [Errno 13] Permission denied: 'C:/Program Files (x86)/madeUpDirectory/madeUpSubdirectory/plugins/madeUpFile.dll'
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:
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));
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.
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.
I'm missing something, I'm sure - but what? How should I begin to diagnose this problem?
Upvotes: 3
Views: 8780
Reputation: 1345
Try running your python script in a Administrator Command Prompt. Such a Command Prompt can be opened by right clicking on the cmd prompt icon and then running as Administrator.
The error is usually encountered when you are trying to perform a job as a user which is not privileged enough to do the action.
Upvotes: 1
Reputation: 111
Try disabling your antivirus software and make sure all processes that are using the file are completed. If that does not cure it try putting a small delay in before deleting to make sure that the process is completed before attempting to remove.
Upvotes: 1