简体   繁体   中英

PermissionError: [WinError 5] Access denied

I am trying to call a python program with subprocess , but I get a permission error. I tried running PyCharm as an admin, but it doesn't help.

My code:

answer = subprocess.check_output("../folder python program %s %s" %(valueA, valueB), encoding = 'utf8')

The error:

Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/a/b/b_resolution.py", line 35, in <module>
    answer = subprocess.check_output("../folder python program %s %s" %(valueA, valueB), encoding = 'utf8')
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 376, in check_output
    **kwargs).stdout
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 453, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 1155, in _execute_child
    startupinfo)
PermissionError: [WinError 5] Access Denied

Does someone know how I can fix this permission error?

This confusing PermissionError also arises if you (accidentally) try to run a directory , instead of a file .

For example, any of these will raise the PermissionError: [WinError 5] Access is denied :

subprocess.check_output('.')
subprocess.run('.')

On the other hand, if you try to run a non-existent file, you'll get a more useful FileNotFoundError: [WinError 2] The system cannot find the file specified .

I fixed the problem by myself the python command comes before the path. Like this:

answer = subprocess.check_output("python ../folder program %s %s" %(valueA, valueB), encoding = 'utf8')

But I had the problem that it says:

can't find '__main__' module in '../pydig'

Solved that aswell with writing the program name included in the path:

answer = subprocess.check_output("python ../folder/program %s %s" %(valueA, valueB), encoding = 'utf8')

Check the file permissions for your current user.

Right click on the file and in security you can see file permissions for users.

If you haven't permission to read file, Advanced > Select a principal then check this doc.

close file explorer...

dumb but if you have the folder open in the explorer and you're trying to do anything to the folders/files you'll get this error

Don't run in Visual Studio. It happened to me just now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM