0

I'm trying to pass arguments to my test_script.py but I'm getting the following error. I know this isn't the best way to do this but it's the only one that will work since I won't know what functions are in test_script.py. How can I pass arguments as stdin input?

test_script.py

a = int(input()) b = int(input()) print(a+b) 

main_script.py

try: subprocess.check_output(['python', 'test_script.py', "2", "3"], stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: print(e.output) 

Error

b'Traceback (most recent call last):\r\n File "test_script.py", line 1, in <module>\r\n a = int(input())\r\nEOFError: EOF when reading a line\r\n' 
8
  • in main_script you are passing numbers as argument but in test_script you are trying to read them from stdin Commented Nov 28, 2015 at 20:34
  • Yes, I know that, but I don't know how to fix it Commented Nov 28, 2015 at 20:52
  • what are you actually trying to do? Commented Nov 28, 2015 at 21:02
  • A script that test other scripts, you type input data and read the output, an if you know correct output you can see if the script is working properly Commented Nov 28, 2015 at 21:10
  • The dupe covers pretty much all you need Commented Nov 28, 2015 at 21:13

3 Answers 3

1

If do not want to use argv, however is odd, consider Popen and operating/communicating on stdin/stdout

from subprocess import Popen, PIPE, STDOUT p = Popen(['python', 'test_script.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) p_stdout = p.communicate(input=b'1\n2\n')[0] # python 2 # p_stdout = p.communicate(input='1\n2\n')[0] print(p_stdout.decode('utf-8').strip()) # python2 # print(p_stdout) 

As a reference from SO Python subprocess and user interaction.

And even more info on https://pymotw.com/2/subprocess/

Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting error: p_stdout = p.communicate(input='1\n2\n')[0] File "C:\Python34\lib\subprocess.py", line 959, in communicate stdout, stderr = self._communicate(input, endtime, timeout) File "C:\Python34\lib\subprocess.py", line 1195, in _communicate self.stdin.write(input) TypeError: 'str' does not support the buffer interface
edited to be compatibile with python3
-1

Not sure what are you trying to do but here is one working example:

import sys # print('Number of arguments:', len(sys.argv), 'arguments.') # print('Argument List:', str(sys.argv)) # print(sys.argv[1]) # print(sys.argv[2]) a = int(sys.argv[1]) b = int(sys.argv[2]) print(a+b) 

And your main_script.py:

import subprocess try: out = subprocess.check_output(['python', 'test_script.py', "2", "3"], stderr=subprocess.STDOUT) print(out) except subprocess.CalledProcessError as e: print(e.output) 

1 Comment

This works but it uses sys.argv, is there any way for this to work with input(), or to change input() to sys.argv
-1

That is jot going to work, test_script.py expects for keyboard input not the argument.

If you want main_script.py to pass arguments to test_script.py you have to amend test_script.py below code should do the trick

import sys args = sys.argv[1:] for arg in args: print arg 

otherwise you can chek argparse https://docs.python.org/2/library/argparse.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.