如何等待stdout写完文件

lia*_*000 2 python

我试图等待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()
Run Code Online (Sandbox Code Playgroud)

所以这里的问题是program.exe启动并将输出保存到stdout所花费的时间可能需要一些时间,有时sessionID.txt文件为空,因为它需要太长时间.在这里,我想确保stdout已写入sessionID.txt.

任何帮助是极大的赞赏.非常感谢TMARZI

Céd*_*ien 5

启动后使用process.wait()(如Popen文档中所述)方法,它将等待执行结束....