[英]wait for terminal command to finish in python
I have a python script that I can run using the terminal. 我有一个可以在终端上运行的python脚本。 However, I need to wait until it finishes (outputs two text files) before I can continue.
但是,我需要等到它完成(输出两个文本文件)后才能继续。 I implemented:
我实现了:
command=['python','segment.py','audio.wav','trans.txt','out1.txt','out2.txt']
cmd=subprocess.Popen(command).wait()
it does generate out1.txt and out2.txt almost immideatly but they are empty. 它确实几乎立即生成out1.txt和out2.txt,但它们为空。 When I run the command from the terminal I get the correct out1.txt and out2.txt (after a few minutes).
当我从终端运行命令时,会得到正确的out1.txt和out2.txt(几分钟后)。 Should I use some other operating commands?
我应该使用其他一些操作命令吗?
use communicate: 使用沟通:
cmd = subprocess.Popen(command)
cmd.communicate()
# Code to be run after execution finished
Try using os.system
. 尝试使用
os.system
。 This will wait for the command to finish, and returns the exit status of 0 if it runs successfully. 这将等待命令完成,如果成功运行,则返回退出状态0。 To get the meaning of other exit statuses, use
os.strerror(exit_status)
. 要获取其他退出状态的含义,请使用
os.strerror(exit_status)
。
See https://docs.python.org/3.5/library/os.html#os.system and https://docs.python.org/3.5/library/os.html#os.strerror for more help. 请参阅https://docs.python.org/3.5/library/os.html#os.system和https://docs.python.org/3.5/library/os.html#os.strerror以获取更多帮助。
Example: 例:
os.system('python segment.py audio.wav trans.txt out1.txt out2.txt')
>>> 0
Please note that the argument for os.system
must be of type str
. 请注意,
os.system
的参数必须为str
类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.