【发布时间】: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