4

I am using Popen to run a command but I don't know how I can write a callback that gets called once the command is finished. Any idea?

Thanks. Bin

Bin Chen
  • 61,507
  • 53
  • 142
  • 183
  • 2
    possible duplicate of [Python subprocess: callback when cmd exits](http://stackoverflow.com/questions/2581817/python-subprocess-callback-when-cmd-exits) – Alex Martelli Aug 26 '10 at 03:15

2 Answers2

2

You can call communicate():

 p = subprocess.Popen('find . -name "*.txt"', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 stdout, stderr = p.communicate()

You can also call wait(), but this might cause problems if the child process fills its output buffer.

Jeet
  • 38,594
  • 7
  • 49
  • 56
2

You could use p.poll() method of the Popen object.

http://docs.python.org/library/subprocess.html#subprocess.Popen.poll

Cody Snider
  • 368
  • 3
  • 5