user3651858
user3651858

Reputation: 21

Output from subprocess not saved

When I use stdout in my Popen with stdin my process does not run.

def program():
    p = subprocess.Popen(link, shell=True, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    out = p.communicate(longquery)
    print out

When I run this my out comes out as (None, None) although I do have an output that shows up on the console. Why is this happening and how can I save the output that is coming out?

Upvotes: 0

Views: 687

Answers (2)

SpliFF
SpliFF

Reputation: 38956

You need to pipe the output data per the manual:

"to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too."

def program():
    p = subprocess.Popen(link, shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    out = p.communicate(longquery)
    print out

Upvotes: 1

James Mills
James Mills

Reputation: 19030

You need to set stdout=PIPE as well.

Change your code to:

from subprocess import Popen, PIPE, STDOUT


def program():
    p = Popen(link, shell=True, stdin=PIPE, stderr=STDOUT, stdout=PIPE)
    out = p.communicate(longquery)
    print out

Upvotes: 0

Related Questions