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

标签 python

我正在尝试等待标准输出完成写入文件。有人对我如何做到这一点有任何想法吗?

这是一段代码:

代码:全选

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/

相关文章:

python - 剖析 CherryPy

python - Wagtail 管理员,CheckboxSelectMultiple 不保存数据

python - MySQL 连接器/Python : How to use the start_transaction() method?

选择第一个计算结果为 True 的变量的 Pythonic 方法

python - 在 python matplotlib 中绘制 3d 直方图/barplot

python - 如何在不阻塞主线程的情况下执行 "fire and forget"任务?

python - 如何绘制由 python 中的向量给出的结构的表面?

python - 在 Python 中等同于 R 的 sapply 条件

python - App Engine 应用程序设计问题

python - 在Python中简单添加