I am trying to open a python script from another python script using Popen in Python 2.7.
The two scripts are:
child.py: Takes 5 ints one by one and waits some time and prints its square
import time
for i in range(5):
value = int(raw_input('Enter an integer: '))
time.sleep(2)
print "Its square is ", value*value
parent.py: Opens child.py and writes 5 ints to its stdin and prints its stdout
import subprocess, time
# following is the line of interest
child_program = subprocess.Popen("child.py",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
for i in range(5):
child_program.stdin.write(str(i)+"\n")
child_program.stdin.flush()
a = child_program.stdout.readline()
print [a] # I put it in list just to see exact format
The following are the other substitutions that I used for the first argument of Popen constructor in parent.py after seeing many similar question in stackoverflow
"./child.py": Same WindowsError is produced
<full path>: Same WindowsError is produced
["python", "child.py"]: Did not raise error but opened python (useless)
The WindowsError produced is:
WindowsError: [Error 193] %1 is not a valid Win32 application