gpt4 book ai didi

python - 如何等到 stdout 完成写入文件

转载 作者:行者123 更新时间:2023-11-28 22:04:28 26 4
gpt4 key购买 nike

我正在尝试等待 stdout 完成写入文件。有人知道我该怎么做吗?

这是一段代码:

代码:全选

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 启动并将输出保存到标准输出所需的时间可能需要时间,有时 sessionID.txt 文件是空的,因为它花费的时间太长。在这里,我想确保 sessionID.txt 已被标准输出写入。

非常感谢任何帮助。非常感谢天玛姿

最佳答案

启动后使用process.wait() ( as explained in the Popen documentation ) 方法,等待执行结束....

关于python - 如何等到 stdout 完成写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com