Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-psutil Windows权限错误?_Python_Windows_Python 3.x_Windows 7_Permissions - Fatal编程技术网

Python-psutil Windows权限错误?

Python-psutil Windows权限错误?,python,windows,python-3.x,windows-7,permissions,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 wr

我试图使用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)
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
来自评论:

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)

…再进一步搜索,这似乎是作者不愿解决的问题(太复杂):。这个答案看起来是最好的方法。但是没有PermissionError,所以只需捕获AccessDenied(除psutil.AccessDenied:#windows之外的AccessDenied)

范例

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))) 

参考:


psutil.AccessDenied在列表理解中可能有用法?

这里有更多信息(关于拒绝访问):如果您在尝试访问proc.name时被拒绝访问,您仍然无法访问proc.name,当您捕获异常时。事实上,我先尝试了psutil.process\u iter(),我也遇到了同样的错误。我只是在事后使用get_process_list()来尝试解决这个问题。所以我不认为这能解决我的案子。。。感谢您的回复……此外,
AccessDenied
似乎不是我可用的变量。我希望可能是通过尝试/但可能问题只是一个特定的过程(但不是我正在研究的过程)。但是,PermissionError本身无法捕获问题。有什么想法吗?…再进一步搜索,这似乎是作者不会解决的问题(太复杂):。这个答案看起来是最好的方法。但是没有
PermissionError
,所以只需捕获AccessDenied