Python 从子进程调用 raw_input [英] Python calling raw_input from a subprocess

查看:38
本文介绍了Python 从子进程调用 raw_input的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用子进程从下面的脚本调用一个 python 脚本.从命令行,用户使用 raw_input

I'm calling a python script from the below one using subprocess. From the command line the user chooses which file to open using 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()

但是在子进程中调用(比如说 option_1.py 被调用)我再次使用 raw_input 来接受来自用户的提示时遇到了问题.我知道 .PIPE 参数并尝试过

However on the subprocess called(say option_1.py is called) I have a problem using raw_input again to accept prompt from the user. I am aware of the .PIPE arguments and have tried

subprocess.Popen(file, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

但还是没有运气.

推荐答案

以下是子进程接收我输入的示例:

Here's an example where the subprocess receives my input:

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()

这篇关于Python 从子进程调用 raw_input的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆