2

I have a script that removes and substitute some files in C:\Windows directory.

I start command line as admin then I start my python script in it. And when the script tries to remove files from C:\Windows, I get WindowsError error 5.

How can I fix this?

Possible solution: Actually I was trying to modify files which has all privileges only for TrustedInstaller user, so I used this https://github.com/jschicht/RunAsTI to run python script.

3
  • Did you try using "runas" cmd command? Commented Aug 12, 2017 at 9:53
  • You're an administrator, so you can take ownership and grant yourself whatever permissions you want. You can use subprocess.call to run takeown.exe and icacls.exe. Commented Aug 13, 2017 at 2:33
  • answer at stackoverflow.com/questions/19672352/… Commented Nov 15, 2019 at 5:39

1 Answer 1

1

WindowsError error 5 occurs when you have no System Administrator privileges to perform action.
You can try to force script to run with admin priveleges with Windows cmd command runas.
Try something like this:

runas /user:administrator_account path_to_script

Just replace administrator_account with account name that has privileges on your computer. Also this command will prompt for password (if account has one setup). For more information about this command you can read here.

Sign up to request clarification or add additional context in comments.

4 Comments

With the default UAC and policy settings nowadays, runas.exe will only work the way you expect with the "Administrator" account (RID 500), which has to be manually enabled. Other administrator accounts are logged on with a split token, and runas.exe will create the process using the standard token instead of the elevated token.
But the OP claims to be working from an elevated command prompt already, so Python should already have admin rights, in which case the OP may be trying to delete a memory-mapped file, such as a loaded DLL, running executable, or mapped data file. The memory manager doesn't allow deleting memory-mapped files.
You are totally right, forgot about that thanks for note
Thank you for answers, but I found that files that I need to modify has all privileges only for TrustedInstaller (I updated my question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.