Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 270
R__raki__ Avatar asked Feb 04 '26 18:02

R__raki__


1 Answers

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)
>>> 
like image 196
Daniel Avatar answered Feb 06 '26 07:02

Daniel