views:

1172

answers:

6

I'm trying to have python delete some directories and I get access errors on them. I think its that the python user account doesn't have rights?

WindowsError: [Error 5] Access is denied: 'path'

is what I get when I run the script.
I've tried

shutil.rmtree  
os.remove  
os.rmdir

they all return the same error.

+4  A: 

I've never used Python, but I would assume it runs as whatever user executes the script.

Max Schmeling
ok, so it apparently runs as me, why can't I delete the file through python, but I can if I just delete it through rmdir on the command line?
DevelopingChris
Do you just have the path hard coded? Are you sure it's the correct path? I can't think of any reason why it wouldn't work if you can do it manually.
Max Schmeling
yeah, I had it dynamically getting the path from a listdir, but then when that got hosed up, I just put the same path in python, setup the scenario and python can't delete it but I can if I use cmd
DevelopingChris
I really don't know what could be the issue then. I'm guessing the Access denied error is just disguising the real issue, unless Python runs methods like that in some kind of sandbox. Sorry I don't have better answers.
Max Schmeling
A: 

How are you running the script? From an interactive console session? If so, just open up a DOS command window (using cmd) and type 'whoami'. That is who you are running the scripts interactively.

Ok I saw your edits just now...why don't you print the path and check the properties to see if the user account running the scripts has the required privileges?

If whoami does not work on your version of Windows, you may use the environment variables like SET USERNAME and SET DOMAINNAME from your command window.

msvcyc
whoami is a *nix command, not windows.
Max Schmeling
@Max--have you tried it on Windows? It works just fine.
Iceman
I just tried it, it doesn't work for me
Max Schmeling
`whoami` not found on my XP at work.
Mark Rushakoff
I'm guessing something you've installed added a whoami command.
Max Schmeling
whoami comes with the gnu port, so does ls, its the only thing that makes cmd bearable
DevelopingChris
@Max: whoami works fine on my version of WIndows which is 2003 server. what version of windows are u running? Don't just provide a blanket comment saying it is a *NIX command and do not down vote if you are not sure about something. Please ask the community.
msvcyc
btw, whoami, says me, but I can manually delete the directory with rmdir /S, why can't python?
DevelopingChris
@msvcyc Are you running 'whoami' in cmd or in powershell?
Max Schmeling
FWIW, my WinXP Pro x4 setup have a whoami.exe in SYSTEM32 that's copyright by Microsoft. I honestly can't say if it's a default part of the system or some ther toolset installed it (maybe a resource kit or something). I doubt it's a default part of the system, since I know I often try to use it on systems where it's not found (which is pretty irritating).
Michael Burr
Provide a ****** reason before down voting. That way I'll also learn if I am wrong.
msvcyc
@Max: whoami is available by default in vista, 2k8 and is part of service packs in 2000, 2003 and possibly XP pro (I am not sure)
msvcyc
@msvcyc: XP pro doesn't have whoami command unless you install external tools like cygwin or something.
bLee
For what its worth, whoami is standard on at least Vista and Windows 7. Although, its almost as easy to just type ECHO %USERDOMAIN%\%USERNAME% to get the same output on other Windows versions.
Andre Miller
A: 

If the script is being run as a scheduled task (which seems likely for a cleanup script), it will probably run as SYSTEM. It's (unwise, but) possible to set permissions on directories so that SYSTEM has no access.

pzr
Scheduled tasks run as whatever user you tell it to run as. Though, I think on older versions of windows this isn't true.
Max Schmeling
A: 

Are the directories empty, and if not do those methods support decursively deleting the contents of a directory?

Kragen
+2  A: 

The scripts have no special user, they just run under the currently logged-in user which executed the script.

Have you tried checking that:

  • you are trying to delete a valid path? and that
  • the path has no locked files?
Yuval A
+6  A: 

We've had issues removing files and directories on Windows, even if we had just copied them, if they were set to 'readonly'. shutil.rmtree() offers you sort of exception handlers to handle this situation. You call it like this:

shutil.rmtree(filename, ignore_errors=False, onerror=handleRemoveReadonly)

and provide an exception handler like this:

def handleRemoveReadonly(func, path, exc):
  excvalue = exc[1]
  if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
      os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
      func(path)
  else:
      raise

You migh want to try that.

ThomasH
Also see the `onerror` function in http://www.voidspace.org.uk/downloads/pathutils.py
Sridhar Ratnakumar