Peter Ezzat
Peter Ezzat

Reputation: 43

subprocess.Popen doesn't run the bash command?

I have tried using subprocess .Popen in Python IDLE, in both versions 2.7 and 3.2 and the code didn't work!!!

The Python Code

Thank you.

Upvotes: 1

Views: 1433

Answers (1)

glglgl
glglgl

Reputation: 91017

I suspect that IDLE redirects Python's stdout so that printing happens in the window, but it doesn't redirect file descriptor 1, so that any subprocess happily prints to that file descriptor.

Try to start IDLE from the command line, run these lines and see if their output is printed to the terminal where IDLE is called from.

Solutions:

  1. Don't use IDLE. Call your stuff from a python interpreter called from the command line.
  2. Redirect the output of your processes to a pipe, read this pipe and output it. Such as:

    sp = subprocess.Popen(['ls'], stdout=subprocess.PIPE)
    for i in sp.stdout: print i
    sp.wait()
    

Upvotes: 2

Related Questions