1

For example, the shell script takes an integer at a prompt and returns it.

Enter an integer:
--> 3
3

I'm using subprocess.check_call(["./myScript"]) to run the shell script. How can I automate sending the "3" in as in the example above? So far all my searching has only recovered how to run a script with command line arguments, not this kind of manual input.

2
  • How do you get the input (the "3")? Are you using input(), raw_input() ect.? And what Python version do you have? Commented May 9, 2016 at 13:07
  • OP is not taking input from python, he/she wants to pass input from python to shell script that prompts for input Commented May 9, 2016 at 13:10

2 Answers 2

1

As the earlier answer explained subprocess.Popen can be used to create process that can be interacted with communicate. communicate takes string as a parameter that will be passed to the created process and returns tuple (stdout, stderr). Below is a short example of two Python scripts communicating with it:

Child

nums = raw_input()
print sum((int(n) for n in nums.split()))

Parent

import subprocess

p = subprocess.Popen(['python', 'test.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = p.communicate('3 4 5')
print 'From other process: ' + out

Output

From other process: 12
Sign up to request clarification or add additional context in comments.

Comments

0

You probably want to use the subprocess.Popen.communicate() function. The docs are quite expressive.

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.