本文介绍了Python - psutil Windows 权限错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 psutil 在 Windows 7 上获取进程的 PID,但我遇到了权限错误.我试过运行以管理员身份运行脚本的命令提示符,但这似乎没有任何效果.错误和相关代码都在下面.发生错误的那一行是在尝试使用 proc.name 访问进程名称时.关于我如何解决这个问题的任何建议?非常感谢!

I'm attempting to get the PID of a process on Windows 7 using psutil, but I'm running into a permissions error. I've tried running the Command Prompt which is running the script as administrator, but this doesn't seem to have any effect. Both the error and the relevant code is below. The line that the error occurs on is when trying to access the process name using proc.name. Any suggestions on how I might fix this? Thank you much!

错误:

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)

推荐答案

get_process_list() 不推荐使用 psutil.process_iter() 0.6.0 .同样在最新的 psutil 中,这个问题似乎已解决.您还可以继续迭代流程:

get_process_list() is deprecated use psutil.process_iter() 0.6.0 .Also in newest psutil this problem seems to be fixed.You can also continue iterating over processes:

for proc in psutil.process_iter():
   try:
       if proc.name == PROCNAME:
          print(proc)
   except (PermissionError, AccessDenied):
       print "Permission error or access denied on process" # can't display name or id here

来自评论:

...搜索更多,似乎这是作者不会解决的问题(太复杂):http://groups.google.com/forum/#!topic/psutil/EbdkIGlb4ls.这个答案看起来是最好的方法.但是没有 PermissionError,所以只需捕获 AccessDenied

...and searching more, it appears that this is an issue that the author won't fix (too complex): http://groups.google.com/forum/#!topic/psutil/EbdkIGlb4ls. This answer looks like the best way to do this. There is no PermissionError though, so just catch AccessDenied

这篇关于Python - psutil Windows 权限错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:04
查看更多