【问题标题】:WindowsError [error 5] Access is deniedWindowsError [错误 5] 访问被拒绝
【发布时间】:2011-03-01 15:08:57
【问题描述】:

我正在使用 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

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

谢谢

【问题讨论】:

    标签: python subprocess windowserror


    【解决方案1】:

    或者,如果您的模块不起作用,您可以使用 «subprocess» 模块:

    import subprocess, os, time
    
    process = subprocess.Popen("somecommand", shell=True)
    n = 0
    while True:
        if not process.poll():
            print('The command is running...')
            if n >= 10:
                pid = process.pid
                os.kill(pid, 9) # 9 = SIGKILL
        else:
            print('The command is not running..')
        n += 1
        time.sleep(1) 
    

    【讨论】:

    • 取出process.pid()上的括号("TypeError: 'int' object is not callable")
    【解决方案2】:

    确保您的路径包含可执行文件的名称 (inkscape.exe)

    【讨论】:

    • 很难抓到这个!
    【解决方案3】:

    我在使用 subprocess 模块遇到此问题时发现,“args”中的第一个条目(subprocess.Popen() 的第一个参数)需要只是没有路径的可执行名称,我需要设置 executable在我的可执行文件的完整路径的参数列表中。

    app = 'app.exe'
    appPath = os.path.join(BIN_DIR, app)
    
    commandLine = [app, 'arg1', 'arg2']
    process = subprocess.Popen(commandLine, executable=appPath)
    

    【讨论】:

    • 也要注意当前工作目录;其他答案建议在开始流程之前需要os.chdir(other_dir),这可能是正确的,具体取决于流程本身的实施。但是,您也可以只使用 Popencwd=other_dir 参数来设置 cwd,而无需为脚本本身更改它。
    • executable=... 根本不适合我,但 os.chdir() 可以。 cwd= 也可以,对我来说这是一个最好的答案。
    【解决方案4】:

    我通过切换到进程目录(我尝试使用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])
    

    也许切换到进程目录也对你有用。

    【讨论】:

      【解决方案5】:

      您是否指定完整路径到您传递给Popenargv 中的第一项)的可执行文件?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-03
        • 2016-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-02
        相关资源
        最近更新 更多