Python - psutil Windows权限错误?

时间:2013-11-01 17:08:25

标签: python windows python-3.x windows-7 permissions

我试图使用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)

2 个答案:

答案 0 :(得分:1)

不推荐使用get_process_list()使用psutil.process_iter()0.6.0。 在最新的psutil中,这个问题似乎也得到了解决。 您还可以继续迭代进程:

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

答案 1 :(得分:0)

除了psutil.AccessDenied:#Windows

示例

def test_children_duplicates(self):
        # find the process which has the highest number of children
        table = collections.defaultdict(int)
        for p in psutil.process_iter():
            try:
                table[p.ppid()] += 1
            except psutil.Error:
                pass
        # this is the one, now let's make sure there are no duplicates
        pid = sorted(table.items(), key=lambda x: x[1])[-1][0]
        p = psutil.Process(pid)
        try:
            c = p.children(recursive=True)
        except psutil.AccessDenied:  # windows
            pass
        else:
            self.assertEqual(len(c), len(set(c))) 

参考:https://www.programcreek.com/python/example/53869/psutil.process_iter

def find_process(regex):
    "If 'regex' match on cmdline return number and list of processes with his pid, name, cmdline."
    process_cmd_name = re.compile(regex)
    ls = []
    for proc in psutil.process_iter(attrs=['pid','name','cmdline']):
        try:
            if process_cmd_name.search(str(" ".join(proc.cmdline()))):
                ls.append(proc.info)
        except psutil.AccessDenied:  # windows
            pass
    return (ls)

结合使用psutil.AccessDenied,列表推导中可能会使用吗?