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

2024-10-03 17:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些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

但是,如果我不指定'stdout'参数,subprocess.call()将正常启动。

我可以看到pythonw.exe可能正在重定向输出本身,但我不明白为什么我被阻止为新线程指定stdout。


Tags: 代码defstdoutexceptioncallexesomefilesubprocess