What user do python scripts run as in windows?
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?
Related videos on Youtube
DevelopingChris
I'm a software developer mainly doing web based applications.
Updated on January 10, 2020Comments
-
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 triedshutil.rmtree os.remove os.rmdir
they all return the same error.
-
Rich almost 15 yearsWell, 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 almost 15 yearsI'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 almost 15 yearsTry 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 almost 15 yearsWould 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 almost 15 yearswhoami is a *nix command, not windows.
-
Kevin almost 15 years@Max--have you tried it on Windows? It works just fine.
-
Max Schmeling almost 15 yearsI just tried it, it doesn't work for me
-
Ralph Sinsuat almost 15 years
whoami
not found on my XP at work. -
Max Schmeling almost 15 yearsI'm guessing something you've installed added a whoami command.
-
Max Schmeling almost 15 yearsScheduled tasks run as whatever user you tell it to run as. Though, I think on older versions of windows this isn't true.
-
DevelopingChris almost 15 yearswhoami comes with the gnu port, so does ls, its the only thing that makes cmd bearable
-
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 almost 15 yearsbtw, whoami, says me, but I can manually delete the directory with rmdir /S, why can't python?
-
DevelopingChris almost 15 yearsok, 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 almost 15 yearsDo 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 almost 15 years@msvcyc Are you running 'whoami' in cmd or in powershell?
-
Michael Burr almost 15 yearsFWIW, 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 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 almost 15 yearsyeah, 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 almost 15 years@msvcyc: XP pro doesn't have whoami command unless you install external tools like cygwin or something.
-
Max Schmeling almost 15 yearsI 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 almost 15 yearsFor 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 about 14 yearsAlso see the
onerror
function in voidspace.org.uk/downloads/pathutils.py -
fIwJlxSzApHEZIl about 12 yearsI'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 about 12 yearsI 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 about 12 years@Johanna This recipe only works for files for which you have chmod permission (e.g. files you own).
-
User about 12 yearsIt's also necessary to import errno
-
kmarsh over 8 yearsAccording to the Python Documention, Exceptions raised by onerror will not be caught so I'm not sure the raise statement means anything.
-
DevelopingChris over 6 yearsThe 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 over 5 yearsUse
func in (os.rmdir, os.unlink, os.remove)
, especially as of python 3.7. I couldn't find whereos.remove
is passed to the error handler anywhere as of 3.7. "This function is semantically identical toremove()
" - python os.unlink -
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 thatshutil.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.