Paweł Jaworowski
Paweł Jaworowski

Reputation: 253

Popen.communicate() returns (None, None) even if script print results

I have problem with Popen.communicate().

I have script which return string.

Then I have wrote second script which takes that variable.

v = "./myscript arg1 arg2"
com = subprocess.Popen(v, shell=True).communicate()
print com

com returns (None, None). The point is that I can print inside first script the results, shell print result as well. I can't just assign that print to variable.

Of course first script returns value, not print it.

Upvotes: 11

Views: 8038

Answers (1)

Aran-Fey
Aran-Fey

Reputation: 43276

From the docs:

Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

Hence, create the Popen object with:

subprocess.Popen("./myscript arg1 arg2", shell=True,
                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Upvotes: 12

Related Questions