popen python subprocess,Python subprocess.Popen()等待完成

I am writing a small script to serially walk through a directory and run a command on the subdirectories therein.

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.

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.

What's the best way to accomplish this?

Thank you!

解决方案process = subprocess.Popen(["your_cmd"]...)

process.wait()

Or check_output, check_call which all wait for the return code depending on what you want to do and the version of python.

If you are using python >= 2.7 and you don't care about the output just use check_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值