簡體   English   中英

使用 psutil 獲取“psutil.AccessDenied (pid=6836)”錯誤

[英]Getting 'psutil.AccessDenied (pid=6836)' error using psutil

我想檢查一個程序是否正在運行,所以我搜索並嘗試了這個,以這種方式:

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 平台的解決方案嗎?

您可能需要系統管理員/root 權限。

如果您可以獲得命令提示符,請在運行 python 之前嘗試使用sudo將您的權限提升到 root 用戶。 或授予您的用戶管理員級別的訪問權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM