等待os命令在python中结束[重复]

am46iovg  于 2023-03-20  发布在  Python
关注(0)|答案(2)|浏览(78)

此问题在此处已有答案

Blocking and Non Blocking subprocess calls(1个答案)
5天前关闭。
在我的python程序中,我正在运行:

os.popen('sh download.sh')

其中download.sh使用curl下载了几个csv文件。
有没有办法等到文件下载完毕后,python程序才继续运行?

svmlkihl

svmlkihl1#

您可以通过将命令定义为子进程来解决此问题:

import subprocess

p = subprocess.Popen('sh download.sh', stdout=subprocess.PIPE, shell=True)

(output, err) = p.communicate()  

#This makes the wait possible
p_status = p.wait()

#This will give you the output of the command being executed
print("Command output: " + output)
uxhixvfz

uxhixvfz2#

我想你可以使用“subprocess”和process.wait()函数。例如:

import subprocess
your_process_name = subprocess.Popen('sh','download.sh')
your_process_name.wait()

相关问题