5

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

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)

But again no luck.

1
  • I personally would make this as non-interactive as possible. Commented Jan 17, 2010 at 13:47

2 Answers 2

3

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()
Sign up to request clarification or add additional context in comments.

Comments

2

If I understand your question, you want to redirect the current standard input to the subprocess. Which can be done this way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.