2010-01-17 54 views
4

我使用子進程從下面的一個調用python腳本。在命令行中的用戶選擇使用的raw_inputPython調用子進程的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的論點,並試圖

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

但是再次沒有運氣。

+0

我個人會做這個作爲非交互成爲可能。 – 2010-01-17 13:47:42

回答

3

這裏就是子進程收到我輸入一個例子:

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

如果我理解你的問題,你想將當前標準輸入重定向到子進程。這是可以做到這樣:

subprocess.Popen(file, stdin=sys.stdin, ...)