[英]wait for terminal command to finish in python
我有一個可以在終端上運行的python腳本。 但是,我需要等到它完成(輸出兩個文本文件)后才能繼續。 我實現了:
command=['python','segment.py','audio.wav','trans.txt','out1.txt','out2.txt']
cmd=subprocess.Popen(command).wait()
它確實幾乎立即生成out1.txt和out2.txt,但它們為空。 當我從終端運行命令時,會得到正確的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.system和https://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
類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.