我试图使用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)发布于 2013-11-01 17:21:42
get_process_list()不推荐使用psutil.process_iter() 0.6.0。此外,在最新的,直到这个问题似乎是固定的。您还可以继续迭代过程:
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来自评论的:
...and搜索更多,这似乎是作者不会修复(太复杂)的一个问题:http://groups.google.com/forum/#!topic/psutil/EbdkIGlb4ls。这个答案看起来是最好的方法。但是没有PermissionError,所以只需要捕获AccessDenied
发布于 2019-12-12 18:24:03
除了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))) 参考文献: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的联想中,列表理解中是否有可能使用?
https://stackoverflow.com/questions/19731665
复制相似问题