What user do python scripts run as in windows?

38,083

Solution 1

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 and provide an exception handler like this:

import errno, os, stat, shutil

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

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

You might want to try that.

Solution 2

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

Solution 3

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?
Share:
38,083

Related videos on Youtube

DevelopingChris
Author by

DevelopingChris

I'm a software developer mainly doing web based applications.

Updated on January 10, 2020

Comments

  • DevelopingChris
    DevelopingChris over 4 years

    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.

    • Rich
      Rich almost 15 years
      Well, with which user account do you run the script? Usually you should know, at least on your machines and if you set up the script to run ...
    • DevelopingChris
      DevelopingChris almost 15 years
      I'm just running it as myself, and I can delete the directly manually, so I'm thinking its a weird windows and python permissions disconnect
    • Michael Burr
      Michael Burr almost 15 years
      Try running the script with SysInternals' "Process Monitor" to see exactly which object (file, directory or whatever) the error occurs on and what process it occurs in. "Process Explorer" can tell you what crededtials the process is running under (maybe Process Monitor can too; I'm not sure).
    • mathStudent001
      mathStudent001 almost 15 years
      Would you mind printing out the path you're trying to delete and posting the output here? It could be a problem with the format the path is specified in.
  • Max Schmeling
    Max Schmeling almost 15 years
    whoami is a *nix command, not windows.
  • Kevin
    Kevin almost 15 years
    @Max--have you tried it on Windows? It works just fine.
  • Max Schmeling
    Max Schmeling almost 15 years
    I just tried it, it doesn't work for me
  • Ralph Sinsuat
    Ralph Sinsuat almost 15 years
    whoami not found on my XP at work.
  • Max Schmeling
    Max Schmeling almost 15 years
    I'm guessing something you've installed added a whoami command.
  • Max Schmeling
    Max Schmeling almost 15 years
    Scheduled tasks run as whatever user you tell it to run as. Though, I think on older versions of windows this isn't true.
  • DevelopingChris
    DevelopingChris almost 15 years
    whoami comes with the gnu port, so does ls, its the only thing that makes cmd bearable
  • msvcyc
    msvcyc almost 15 years
    @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.
  • DevelopingChris
    DevelopingChris almost 15 years
    btw, whoami, says me, but I can manually delete the directory with rmdir /S, why can't python?
  • DevelopingChris
    DevelopingChris almost 15 years
    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?
  • Max Schmeling
    Max Schmeling almost 15 years
    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
    Max Schmeling almost 15 years
    @msvcyc Are you running 'whoami' in cmd or in powershell?
  • Michael Burr
    Michael Burr almost 15 years
    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).
  • msvcyc
    msvcyc almost 15 years
    @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)
  • DevelopingChris
    DevelopingChris almost 15 years
    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
  • codingbear
    codingbear almost 15 years
    @msvcyc: XP pro doesn't have whoami command unless you install external tools like cygwin or something.
  • Max Schmeling
    Max Schmeling almost 15 years
    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.
  • mathStudent001
    mathStudent001 almost 15 years
    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.
  • Sridhar Ratnakumar
    Sridhar Ratnakumar about 14 years
    Also see the onerror function in voidspace.org.uk/downloads/pathutils.py
  • fIwJlxSzApHEZIl
    fIwJlxSzApHEZIl about 12 years
    I'm getting: NameError: name 'stat' is not defined EDIT: Requires that you do 'import stat' at the top of your script. Did solve my problem though. Many thanks!
  • Johanna
    Johanna about 12 years
    I tried this solution, now I have the [Error 5] Access denied error with this line : os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
  • ThomasH
    ThomasH about 12 years
    @Johanna This recipe only works for files for which you have chmod permission (e.g. files you own).
  • User
    User about 12 years
    It's also necessary to import errno
  • kmarsh
    kmarsh over 8 years
    According to the Python Documention, Exceptions raised by onerror will not be caught so I'm not sure the raise statement means anything.
  • DevelopingChris
    DevelopingChris over 6 years
    The difference is, I just created it in the previous line of code. So it 100% exists for certain. But there is an access problem in the OS level adapters of windows.
  • Quinn Dirks
    Quinn Dirks over 5 years
    Use func in (os.rmdir, os.unlink, os.remove), especially as of python 3.7. I couldn't find where os.remove is passed to the error handler anywhere as of 3.7. "This function is semantically identical to remove()" - python os.unlink
  • ThomasH
    ThomasH over 5 years
    @QuinnDirks Yes, this is all python 2.x. Check the appropriate docs for shutil.rmtree and maybe play a bit with it. It all hinges on the function that shutil.rmtree passes into the error handler, the one which failed (which, as the docs say, is platform- and implementaton-dependent). Maybe you want to print out the passed function in the error handler, so you see which function is being used in your environment and your use case.