等待终端命令完成python

问题描述:

我有一个python脚本,我可以使用终端运行。但是,我需要等到它完成(输出两个文本文件),然后才能继续。我执行:等待终端命令完成python

command=['python','segment.py','audio.wav','trans.txt','out1.txt','out2.txt'] 
cmd=subprocess.Popen(command).wait() 

它确实生成out1.txt和out2.txt几乎immideatly,但他们是空的。当我从终端运行命令时,我会得到正确的out1.txt和out2.txt(几分钟后)。我应该使用其他一些操作命令吗?

使用通信:

cmd = subprocess.Popen(command) 
cmd.communicate() 
# Code to be run after execution finished 

尝试使用os.system。这将等待命令完成,并且如果成功运行,则返回0的退出状态。要获得其他退出状态的含义,请使用os.strerror(exit_status)

请参阅https://docs.python.org/3.5/library/os.html#os.systemhttps://docs.python.org/3.5/library/os.html#os.strerror获取更多帮助。

例子:

os.system('python segment.py audio.wav trans.txt out1.txt out2.txt') 
>>> 0 

请注意os.system参数必须是str类型。