Python Forum

Full Version: trying out subprocess in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Folks,
I'm trying to checkout the sub-process functionality of Python and am having some trouble, specifically on the subprocess accepting stdin...

here is my masterPrint.py

import subprocess
#subprocess.call(["python", "slavePrint.py"], shell=True)
#stdout of subprocess is piped to p
#stdin of subprocess is piped to p
p = subprocess.Popen(["python", "slavePrint.py"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
while(1):
#read in string
strng = input("enter a number between 1 and 10")
str = p.communicate(str(strng)+ '\n')[0]
print str
here is my slavePrint.py

 a = input()
print ('slave got ' + str(a))
Here is the error I am getting when I run the code:

C:\Users\oahmad>py -2 masterPrint.py
enter a number between 1 and 102
Traceback (most recent call last):
File "slavePrint.py", line 1, in <module>
a = input()
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
enter a number between 1 and 10
Use raw_input() instead input(). Python 2?
issue was using str, which is a keyword in python...
(Jun-13-2017, 12:01 AM)sblu23 Wrote: [ -> ]issue was using str, which is a keyword in python...
I disagree. If that was the problem, you would have had a different error. This looks more like you're using input() with python2, which calls eval() on whatever you pass (and it thus a terrible thing to use).