当我使用 python.exe 运行它时,我有一些 Python 代码可以正常工作,但如果我使用 pythonw.exe 会失败。
def runStuff(命令行): outputFileName = 'somefile.txt' 输出文件 = 打开(输出文件名,“w”) 尝试: 结果 = subprocess.call(commandLine, shell=True, stdout=outputFile) 除了: print '抛出异常:', str(sys.exc_info()[1]) myThread = threading.Thread(None, target=runStuff, commandLine=['whatever...']) myThread.start()
我得到的信息是:
抛出异常:[错误 6] 句柄无效
但是,如果我不指定 'stdout' 参数, subprocess.call() 可以正常启动。
我可以看到 pythonw.exe 可能正在重定向输出本身,但我不明白为什么我被阻止为新线程指定标准输出。