WindowsError [错误5]访问被拒绝

Ser*_*erg 28 python subprocess windowserror

我正在使用killableprocess包(构建在子进程之上)来运行进程每当我在脚本中运行"killableprocess.Popen(command)"代码时,我都会收到以下错误:

File "killableprocess.py", line 157, in _execute_child
  winprocess.AssignProcessToJobObject(self._job, hp)
File "winprocess.py", line 37, in ErrCheckBool
  raise WinError()
WindowsError [error 5] Access is denied
Exception TypeError: "'NoneType' object is not callable" in <bound method AutoHANDLE.__del__ of <AutoHANDLE object at 0x025D42B0>> ignored
Run Code Online (Sandbox Code Playgroud)

但是当我从python交互式控制台(python 2.6)运行它时它工作正常.这可能意味着当我从脚本运行时存在权限问题,但我不知道如何解决它们.我尝试从我以管理员身份运行的cmd运行脚本,但它没有帮助.尝试寻找类似的帖子,但没有找到任何好的解决方案.希望在这里找到一些帮助我在Windows上运行,特别是Windows 7 Ultimate x64,如果有任何帮助的话.

谢谢

tjb*_*tjb 14

我通过切换到进程目录(我试图使用inkscape)解决了类似的问题,它解决了我的问题

import subprocess
inkscape_dir=r"C:\Program Files (x86)\Inkscape"
assert os.path.isdir(inkscape_dir)
os.chdir(inkscape_dir)
subprocess.Popen(['inkscape.exe',"-f",fname,"-e",fname_png])
Run Code Online (Sandbox Code Playgroud)

也许切换到进程目录也适合你.


das*_*ang 9

在使用子进程模块运行时我发现的是'args'中的第一个条目(第一个参数subprocess.Popen())需要只是没有路径的可执行文件名,我需要executable在参数列表中设置为完整路径可执行文件.

app = 'app.exe'
appPath = os.path.join(BIN_DIR, app)

commandLine = [app, 'arg1', 'arg2']
process = subprocess.Popen(commandLine, executable=appPath)
Run Code Online (Sandbox Code Playgroud)

  • 也要注意当前的工作目录;其他答案建议在启动进程之前需要`os.chdir(other_dir)`,这可能是正确的,具体取决于进程本身的实现。但是,您也可以只使用 `Popen` 的 `cwd=other_dir` 参数来设置 cwd,而无需为脚本本身更改它。 (2认同)

小智 2

或者,如果您的模块不起作用,您可以使用 \xc2\xabsubprocess\xc2\xbb 模块:

\n
import subprocess, os, time\n\nprocess = subprocess.Popen("somecommand", shell=True)\nn = 0\nwhile True:\n    if not process.poll():\n        print('The command is running...')\n        if n >= 10:\n            pid = process.pid\n            os.kill(pid, 9) # 9 = SIGKILL\n    else:\n        print('The command is not running..')\n    n += 1\n    time.sleep(1) \n
Run Code Online (Sandbox Code Playgroud)\n

  • 去掉 `process.pid()` 上的括号(“TypeError: 'int' object is not callable”) (2认同)