Rakesh_K Rakesh_K - 1 month ago 18
Python Question

subprocess.Popen process stdout returning empty?

I have this python code

input()
print('spam')


saved as
ex1.py


in interactive shell

>>>from subprocess import Popen ,PIPE
>>>a=Popen(['python.exe','ex1.py'],stdout=PIPE,stdin=PIPE)

>>> a.communicate()

(b'', None)

>>>


why it is not printing the
spam

Answer

Input expects a whole line, but your input is empty. So there is only an exception written to stderr and nothing to stdout. At least provide a newline as input:

>>> a = Popen(['python3', 'ex1.py'], stdout=PIPE, stdin=PIPE)
>>> a.communicate(b'\n')
(b'spam\n', None)
>>>