PermissionError: [WinError 5] Access denied
Asked Answered
C

6

8

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?

Conquer answered 16/1, 2020 at 20:15 Comment(0)
B
8

Although it doesn't answer the original question, this 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('.')

where '.' represents the path to the current directory, as a minimal example.

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.

Tested with python 3.10.6 on Windows and Ubuntu. On Ubuntu the examples above raise a PermissionError: [Errno 13] Permission denied.

Bagpipe answered 2/12, 2021 at 20:21 Comment(0)
M
6

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

Melliemelliferous answered 31/12, 2022 at 15:59 Comment(0)
L
2

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.

Lithographer answered 16/1, 2020 at 20:33 Comment(1)
The OP is trying to execute a directory named "../folder". Windows fails this with access denied, which in this case has nothing to do with security.Ironwood
C
2

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')
Conquer answered 16/1, 2020 at 20:35 Comment(2)
With an argument list, subprocess will handle any required quoting for you, based on the assumption that the program follows VC++ command-line parsing rules, which python.exe itself certainly uses since it's built with VC++.Ironwood
If you're using UTF-8 mode in 3.7+ (e.g. python -X utf8) or defining the PYTHONIOENCODING environment variable to use UTF-8, then Python will write UTF-8 to a pipe in Windows. Otherwise, by default a Python script in Windows uses the system ANSI codepage (e.g. "cp1252" in a Western Europe locale) when stdout is a pipe, in which case decoding via encoding='utf8' will generally fail for non-ASCII text. In Windows 10, ANSI may be set to UTF-8, but it's not the default setting.Ironwood
F
0

In my case, the error was because the path was not complete.

I changed this:

cmd = r'C:/Program Files (x86)/path'

to this:

cmd = r'C:/Program Files (x86)/path/completepath'

(also check you use the right slash \ or / for windows)

Furnish answered 4/6, 2024 at 17:27 Comment(0)
R
0

If you have opened the requested file in the file explorer close and mostly I mresolve this issue when I reopened VScode as an admin.

Remunerate answered 17/8, 2024 at 8:27 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Gore

© 2022 - 2025 — McMap. All rights reserved.