-1

I want to run a process from a python script, blocking while it runs, and writing the output of the process to stdout.

How would I do this?

I looked at the documentation for 'subprocess' but couldn't work it out.

Editing this question to explain how it's different, as requested: See existing text above: and writing the output of the process to stdout

mackenir
  • 10,801
  • 16
  • 68
  • 100
  • Printing to stdout: http://stackoverflow.com/questions/2082850/real-time-subprocess-popen-via-stdout-and-pipe – martin Mar 03 '16 at 11:59
  • Thanks - waiting until the command is done is one aspect of what I'm after. I could achieve that with `call`, I think (that's what I've found). One extra feature is that I also want the output to be streamed to my console. – mackenir Mar 03 '16 at 12:13
  • why not just use `subprocess.check_call([command])`? – Padraic Cunningham Mar 03 '16 at 12:17

2 Answers2

0

You can use the wait() method for that.

For example:

# main_script.py

#some code

p1 = subprocess.Popen(['/path/to/process.sh'], shell=True) 
p1.wait()

#rest of code when process is done
Avihoo Mamka
  • 4,656
  • 3
  • 31
  • 44
0

you can use object.communicate()[0]

p=subprocess.Popen(["python","1st.py"],stdin=PIPE,stdout=PIPE)
print p.communicate()[0]

Communicate() will hold the last processed process output.
sathish
  • 149
  • 9