我正在尝试等待标准输出完成写入文件。有人对我如何做到这一点有任何想法吗?
这是一段代码:
代码:全选
session = open("c:\\sessionID.txt", "w")
#Execute previous exe and output session to text file from the current directory
cmd = os.path.realpath(os.getcwd()) +"\\Program.exe"
cmd = [os.getcwd() + '\\AOSLaunch.exe']
Execute the cmd which executes the program.exe and the output is saved to the sessionID.txt file
process = subprocess.Popen(cmd, stdout=session)
session.close()
所以这里的问题是,program.exe 启动并将输出保存到 stdout 所需的时间可能需要一些时间,有时 sessionID.txt 文件为空,因为它花费的时间太长。这里我想确保 sessionID.txt 已被 stdout 写入。
非常感谢任何帮助。 非常感谢 TMARZI
最佳答案
启动后使用process.wait()
( as explained in the Popen documentation )方法,它将等待执行结束......
关于python - 如何等待 stdout 完成写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095136/