Python argv and cmd

544 views Asked by At

I'm trying to make a Python program that can correct exams automaticly, I have extra time and don't wanna wait for my teacher to correct them manually... Annyways when i use python argv like this:

import sys

def hello(a):
    print(a)

a = sys.argv[1:]
hello(a)

And i want to insert a list, I can no longer insert just one variable because of the way argv works, and I can't know how long the list will be because not all tasks are the same. I'm using subprocess.check_output to return the program output after my checker runs it in a cmd window... Now if someone knows a better way to approach correcting the programs without making the students replace their input with sys.argv(if there is a better way to input arguments to a seperate python program when you run it) or can tell me how to fix the argv issue?

2

There are 2 answers

0
Vincent On BEST ANSWER

You could use Popen.communicate instead of check_output:

echo.py:

print(input())

test.py:

from subprocess import Popen, PIPE
p = Popen(['python3', 'echo.py'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
out, err = p.communicate(input="hello!".encode())
assert out.decode().strip() == "hello!"
0
dmr On

I would suggest you to look into OptionParser