I want to give score to several python scripts written by different people and i want to automate the answer check since the given question is same. So we send an input to another python file, we get the output (the terminal/console output) then we compare it, all that within a python file (Like hackerrank, uri, or another competitive programming website)
For example the problem is to multiply the given input by 2. Then i have one python script answer_check.py
to automate answer checking, and i have another python script which is one of the answer a.py
.
a.py
:
a= int(input('input a: '))
res= a*2
print(res)
answer_check.py
:
# Some code to send input to a.py
# Then some code to get the console output from a given input
if given_output==desired_output:
score= 100
What i have tried:
- I have read some other stackoverflow post that related to this problem but it is kinda different because either they don't have
input()
in the answer file they want to check, or they do input viasys.args
. - I have tried
pexpect
but but apparently it doesn't apply to windows os - I have tried
wexpect
it is likepexpect
but for windows, but i have an installation problem withpywin32
- I tried
runpy
but we have to input manually - I tried
subprocess
module
from subprocess import Popen, PIPE
p = Popen("python a.py", stdin=PIPE, stdout=PIPE, shell=False)
out = p.communicate(input='1', timeout=5)
print(out)
But it give me this error
File "a.py", line 1, in <module>
a= input('input a: ')
EOFError: EOF when reading a line
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp1252'>
OSError: [Errno 22] Invalid argument
If you know please answer even though it is on another language :)