Skip to main content
 首页 » 编程设计

python之访问被拒绝的 Python 脚本 Pstuil

2024年10月01日55grandyang

所以基本上我已经在使用 Psutil 的 Python 中创建了这个应用程序。我正在使用它以非常高的速度暂停和恢复进程。虽然它确实适用于大多数流程,但对某些流程无效。我将在下面提供错误代码,但如果您决定提供帮助,请对菜鸟非常友好,我才刚刚开始使用 python

import psutil 
import time 
import subprocess 
 
 
 
print("Auto Suspender") 
 
condition = 10 
 
subprocess.call("taskkill /Program example") 
 
print("Program Example has been killed :)") 
 
time.sleep(1) 
 
somepid = int(input("What is Process ID ?")) 
p = psutil.Process(somepid) 
print("Working...") 
time.sleep(0.5) 
 
while condition == 10: 
    choice = input("1__Suspend") 
    if (choice == "1"): 
        while condition == 10: 
            p.suspend() 
            print("Suspended !") 
            time.sleep(0.1) 
            p.resume 
            print("Resumed !") 
    else: 
        print("Invalid response")`enter code here` 

我收到错误

Traceback (most recent call last): 
  File "C:\Python34\lib\site-packages\psutil\_pswindows.py", line 207, in wrapper 
    return fun(self, *args, **kwargs) 
  File "C:\Python34\lib\site-packages\psutil\_pswindows.py", line 366, in suspend 
    return cext.proc_suspend(self.pid) 
PermissionError: [WinError 5] Access is denied 
 
During handling of the above exception, another exception occurred: 
 
Traceback (most recent call last): 
  File "C:/Users/Jacob/PycharmProjects/Suspender.111/Main.py", line 26, in <module> 
    p.suspend() 
  File "C:\Python34\lib\site-packages\psutil\__init__.py", line 273, in wrapper 
    return fun(self, *args, **kwargs) 
  File "C:\Python34\lib\site-packages\psutil\__init__.py", line 1025, in suspend 
    self._proc.suspend() 
  File "C:\Python34\lib\site-packages\psutil\_pswindows.py", line 213, in wrapper 
    raise AccessDenied(self.pid, self._name) 
psutil.AccessDenied: (pid=3076) 
 
Process finished with exit code 1 

请您参考如下方法:

我认为您没有足够的权限暂停进程。 Windows 操作系统(以及其他操作系统)中的某些进程在另一个用户帐户下运行,在 Windows 中通常在系统帐户下,普通用户无法暂停或终止此进程。尝试以管理员用户身份运行您的脚本,脚本可能会正常工作。