我正在从下面的一个子进程中调用一个python脚本。用户从命令行使用raw_input选择要打开的文件
import optparse
import subprocess
import readline
import os
def main():
options = {'0': './option_0.py',
'1': './option_1.py',
'2': './option_2.py',
'3': './option_3.py'}
input = -1
while True:
if input in options:
file = options[input]
subprocess.Popen(file)
else:
print "Welcome"
print "0. option_0"
print "1. option_1"
print "2. option_2"
print "3. option_3"
input = raw_input("Please make a selection: ")
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
但是在被称为(例如option_1.py被调用)的子进程上,我再次使用raw_input接受来自用户的提示时遇到问题。我知道.PIPE参数,并尝试了
subprocess.Popen(file, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
但是再次没有运气。
这是子流程接收我的输入的示例:
import subprocess
import sys
command = 'python -c \'print raw_input("Please make a selection: ")\''
sp = subprocess.Popen(command, shell = True, stdin = sys.stdin)
sp.wait()
Run Code Online (Sandbox Code Playgroud)