5

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
QWERASDFYXCV
  • 245
  • 1
  • 6
  • 15

5 Answers5

4

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.

djvg
  • 11,722
  • 5
  • 72
  • 103
3

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

Uros Randelovic
  • 425
  • 5
  • 6
1

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.

  • 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. – Eryk Sun Jan 17 '20 at 05:09
1

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')
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
QWERASDFYXCV
  • 245
  • 1
  • 6
  • 15
  • 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++. – Eryk Sun Jan 17 '20 at 05:04
  • 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. – Eryk Sun Jan 17 '20 at 05:05
-2

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

zscopuv
  • 1
  • 2
  • 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](/help/how-to-answer). – Community Dec 08 '22 at 13:46