Python subprocess: check if the executed script executed the user request

import subprocess child = subprocess.Popen(['python', 'simple.py'], stdin=subprocess.PIPE) child.communicate('Alice') 

I know that you can communicate with the executed script through communication. How do you check if the script 'simple.py' is asking for user input?

simple.py can request 5-10 user inputs, so just hard coding to communicate will not suffice.

[EDIT]: want to parse stdout when the script starts and return to the script

 while True: if child.get_stdout() == '?': # send user input 
+5
source share
1 answer

A simple example:

simple.py:

 i = raw_input("what is your name\n") print(i) j = raw_input("What is your age\n") print(j) 

Read and write:

 import subprocess child = subprocess.Popen(['python2', 'simple.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) for line in iter(child.stdout.readline, ""): print(line) if "name" in line: child.stdin.write("foo\n") elif "age" in line: child.stdin.write("100\n") 

Output:

 what is your name foo What is your age 100 
+1
source

Source: https://habr.com/ru/post/1244306/


All Articles