Python subprocess.Popen() wait for completion(Python subprocess.Popen() 等待完成)
问题描述
我正在编写一个小脚本来串行遍历目录并在其中的子目录上运行命令.
I am writing a small script to serially walk through a directory and run a command on the subdirectories therein.
我遇到了一个问题,但是 Popen()它将遍历目录并运行所需的命令,而无需等待前一个命令完成.即
I am running into a problem however with Popen() that it will walk through the directories and run the desired command without waiting for the previous one to finish. i.e.
for dir in dirs:
#run command on the directory here.
它会启动每个目录的命令而不关心它.我希望它等待当前一个完成,然后开始下一个.我在目录中使用的工具是来自 SANS SIFT 的 Log2timeline
,它需要相当长的时间并产生相当多的输出.我不关心输出,我只希望程序在启动下一个之前等待.
it kicks off the command for each dir without caring about it afterwards. I want it to wait for the current one to finish, then kick off the next. The tool I am using on the directories is Log2timeline
from SANS SIFT which takes quite a while and produces quite a bit of output. I don't care about the output, I just want the program to wait before kicking off the next.
最好的方法是什么?
谢谢!
推荐答案
使用Popen.等待:
process = subprocess.Popen(["your_cmd"]...)
process.wait()
或 check_output, check_call 等待返回码取决于你想要做什么和python的版本.
Or check_output, check_call which all wait for the return code depending on what you want to do and the version of python.
如果您使用的是 python >= 2.7 并且您不关心输出,只需使用 check_call
.
If you are using python >= 2.7 and you don't care about the output just use check_call
.
您也可以使用 call 但如果您有,则不会引发任何错误可能需要也可能不需要的非零返回码
You can also use call but that will not raise any error if you have a non-zero return code which may or may not be desirable
这篇关于Python subprocess.Popen() 等待完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python subprocess.Popen() 等待完成


基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01