gpt4 book ai didi

python - Python os.popen : How to ensure popen(…) has completed execution before continuing?

转载 作者:行者123 更新时间:2023-12-03 13:16:03 31 4
gpt4 key购买 nike

我有以下代码:

pwd = '/home/user/svnexport/Repo/'

updateSVN = "svn up " + pwd
cmd = os.popen(updateSVN)

getAllInfo = "svn info " + pwd + "branches/* " + pwd + "tags/* " + pwd + "trunk/*"
cmd = os.popen(getAllInfo)

cmd = os.popen(updateSVN)开始执行之前,如何确定 cmd = os.popen(getAllInfo)已经完成执行?

最佳答案

您应该使用subprocess:

import subprocess
import glob
pwd = '/home/user/svnexport/Repo/'

updateSVN = ["svn", "up", pwd]
cmd = subprocess.Popen(updateSVN)
status = cmd.wait()

# the same can be achieved in a shorter way:
filelists = [glob.glob(pwd + i + "/*") for i in ('branches', 'tags', 'trunk')]
filelist = sum(filelists, []) # add them together

getAllInfo = ["svn", "info"] + filelist
status = subprocess.call(getAllInfo)

如果您需要捕获子流程的输出,请执行
process = subprocess.Popen(..., stdout=subprocess.PIPE)
data = process.stdout.read()
status = subprocess.wait()

关于python - Python os.popen : How to ensure popen(…) has completed execution before continuing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21414575/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com