Pythonos.popen公司:如何确保popen(…)在继续之前已完成执行?

2024-09-28 05:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

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)开始执行之前已完成执行?在


Tags: 代码infocmdhomeospwdsvnrepo
3条回答

如果您需要第一个命令终止,那么实际上并不需要多线程。你能做到的

os.system(updateSVN)
os.system(getAllInfo)

如果您真的想使用updateSVN,可以通过

^{pr2}$

尝试wait()方法:

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

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

getAllInfo = "svn info " + pwd + "branches/* " + pwd + "tags/* " + pwd + "trunk/*"
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)

如果您需要捕获子进程的输出,那么应该这样做

^{pr2}$

相关问题 更多 >

    热门问题