我正在尝试使用 psutil 获取 Windows 7 上进程的 PID,但我遇到了权限错误。我试过运行以管理员身份运行脚本的命令提示符,但这似乎没有任何效果。错误和相关代码都在下面。发生错误的行是尝试使用proc.name
. 关于如何解决这个问题的任何建议?非常感谢!
错误:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 190, in wrapper
return fun(self, *args, **kwargs)
File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 229, in get_process_exe
return _convert_raw_path(_psutil_mswindows.get_process_exe(self.pid))
PermissionError: [WinError 5] Access is denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "simple_address_retrieve.py", line 14, in <module>
if proc.name == PROCNAME:
File "C:\Python33\lib\site-packages\psutil\_common.py", line 48, in __get__
ret = self.func(instance)
File "C:\Python33\lib\site-packages\psutil\__init__.py", line 341, in name
name = self._platform_impl.get_process_name()
File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 190, in wrapper
return fun(self, *args, **kwargs)
File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 222, in get_process_name
return os.path.basename(self.get_process_exe())
File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 194, in wrapper
raise AccessDenied(self.pid, self._process_name)
psutil._error.AccessDenied: (pid=128)
代码:
PROCNAME = "MyProcessName.exe"
for proc in psutil.process_iter():
if proc.name == PROCNAME:
print(proc)