For example, program1.py call program2.py:
program1.py
subprocess.Popen("program2.py", stdin=subprocess.PIPE).communicate()
program2.py
user_input = input("Yes or No?")
But when I run program1, the program2 cannot get the user input from the main process in my case. It says "EOF when reading a line". Could you give me some advice to get user input.
You are not passing any data to program2.py
. I assume you are using Python 3, from the use of input
(instead of raw_input
in Python 2). Here is how I would do it:
import subprocess
import sys
cmd = [sys.executable, 'program2.py']
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE)
proc.communicate('Y'.encode())
Passes "Y" through to the second program. The encode()
is required in Python 3, but not Python 2. This is because Python 3 strings are multi-byte, whereas the stdin
stream uses single byte strings, like Python 2 strings. No changes are required at the program2.py
end.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.