代码之家  ›  专栏  ›  技术社区  ›  Charles Anderson

使用pythonw.exe时Python subprocess.call()失败

  •  5
  • Charles Anderson  · 技术社区  · 16 年前

    我有一些Python代码,当我使用Python.exe运行它时可以正常工作,但如果我使用pythonw.exe,则会失败。

        def runStuff(commandLine):
            outputFileName = 'somefile.txt'
            outputFile = open(outputFileName, "w")
    
            try:
                result = subprocess.call(commandLine, shell=True, stdout=outputFile)
            except:
                print 'Exception thrown:', str(sys.exc_info()[1])
    
        myThread = threading.Thread(None, target=runStuff, commandLine=['whatever...'])
        myThread.start()
    

    我得到的信息是:

        Exception thrown: [Error 6] The handle is invalid
    

    3 回复  |  直到 16 年前
        1
  •  7
  •   Piotr Lesnicki    16 年前

    sys.stdin sys.stdout subprocess.call() 我们正在失败。

    不要在意子流程对错误和所有错误的描述,您可以使用 os.devnull

        2
  •  7
  •   Charles Anderson    16 年前

    记录在案,我的代码现在如下所示:

    def runStuff(commandLine):
        outputFileName = 'somefile.txt'
        outputFile = open(outputFileName, "w")
    
        if guiMode:
            result = subprocess.call(commandLine, shell=True, stdout=outputFile, stderr=subprocess.STDOUT)
        else:
            proc = subprocess.Popen(commandLine, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
            proc.stdin.close()
            proc.wait()
            result = proc.returncode
            outputFile.write(proc.stdout.read())
    

        3
  •  2
  •   Eduardo    10 年前

    事实上,对于任何在不使用控制台的情况下将python中的代码转换为exe的框架,都会发生这种情况。

    在我的测试中,我发现如果我在我的规范文件(pyInstaller)中使用标志“console=True”,那么错误就不会再发生了。