38

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!

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
DJMcCarthy12
  • 3,819
  • 8
  • 28
  • 34
  • unrelated: to discard subprocess' output properly, see [How to hide output of subprocess in Python 2.7](http://stackoverflow.com/q/11269575/4279) – jfs Feb 02 '15 at 19:50

2 Answers2

66

Use Popen.wait:

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

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
16
subprocess.check_output( ... )

will block ... and can be used instead of Popen

however if you are set on Popen

subprocess.Popen(...).communicate() 

will also block until the process returns

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 2
    neither `check_output` nor `.communicate()` would be useful here: *"[subprocess] produces quite a bit of output. I don't care about the output"*. `call()` or `check_call()` should be used instead. – jfs Feb 02 '15 at 19:52
  • ahh good point :P thats what I get for not reading ... that said ... the output is just getting tossed out in these examples anyway – Joran Beasley Feb 02 '15 at 19:54