【问题标题】:Getting 'psutil.AccessDenied (pid=6836)' error using psutil使用 psutil 获取“psutil.AccessDenied (pid=6836)”错误
【发布时间】:2020-05-31 15:47:05
【问题描述】:

我想检查一个程序是否正在运行,所以我搜索并尝试了this,这样:

import psutil
if "notepad.exe" in (p.name() for p in psutil.process_iter()):
    print('program is running')
else:
    print('not running')

但我收到此错误:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/temp.py", line 2, in <module>
    if "notepad.exe" in (p.name() for p in psutil.process_iter()):
  File "~/temp.py", line 2, in <genexpr>
    if "notepad.exe" in (p.name() for p in psutil.process_iter()):
  File "~\Python\Python38-32\lib\site-packages\psutil\__init__.py", line 630, in name
    name = self._proc.name()
  File "~\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 750, in name
    return os.path.basename(self.exe())
  File "~\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 681, in wrapper
    raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=6836)

Process finished with exit code 1

我对这个简单的代码做错了吗? 还是有其他替代方法?

编辑:对不起,我忘了在 windows 上提到我,而不是 linux,你能给我 windows 平台的解决方案吗?

【问题讨论】:

  • 您可能需要以提升的权限解释程序。
  • 在 bash 中只需添加 sudo sudo python name.py。在windows中成为管理员然后运行。
  • Windows 10 怎么样?让它强制以管理员身份运行?如果是怎么办?

标签: python python-3.x psutil


【解决方案1】:

您可能需要系统管理员/root 权限。

如果您能获得命令提示符,请在运行 python 之前尝试使用sudo 将您的权限提升到 root 用户。或授予您的用户管理员级别的访问权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 2023-03-05
    • 2017-04-22
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多