简体   繁体   English

PermissionError: [WinError 5] 访问被拒绝

[英]PermissionError: [WinError 5] Access denied

I am trying to call a python program with subprocess , but I get a permission error.我正在尝试使用 subprocess 调用subprocess程序,但出现权限错误。 I tried running PyCharm as an admin, but it doesn't help.我尝试以管理员身份运行 PyCharm,但没有帮助。

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 .如果您(意外)尝试运行目录而不是文件,也会出现这种令人困惑的PermissionError

For example, any of these will raise the PermissionError: [WinError 5] Access is denied :例如,其中任何一个都会引发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 .另一方面,如果你尝试运行一个不存在的文件,你会得到一个更有用的FileNotFoundError: [WinError 2] The system cannot find the file specified

I fixed the problem by myself the python command comes before the path.我自己解决了这个问题,python命令在路径之前。 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.不要在 Visual Studio 中运行。 It happened to me just now.刚才发生在我身上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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