简体   繁体   中英

Running another python program from python

I am trying to build a test script to test my python code.

The tested code should read from stdin and write to stdout.

The tester code should call the tested code passing the values in a file as stdin, and read stdout to compare with the expected values (stored in another file)

Tested code:

n = int(input())
x1,y1 = list(map(int, input().rstrip().split())) 
x2,y2 = list(map(int, input().rstrip().split())) 

#what it does is really not importante
if ((min(x1, x2) <= n/2) and (max(x1, x2) > n/2) or
    (min(y1, y2) <= n/2) and (max(y1, y2) > n/2)):
    print('S')
else :
    print ('N')

Tester code:

import os
import subprocess as sp
inputs = os.listdir("./warmup_tests/warmup_B/input")
for ipt in inputs:
    with open('./warmup_tests/warmup_B/input/{}'.format(ipt)) as f:
        res = (sp.run(['python', 'b.py'], stdin=f, capture_output=True))

I have received multiple of the following error (when disabling capture_output to better visualization):

Traceback (most recent call last):
  File "b.py", line 10, in <module>
    x1,y1 = list(map(int, input().rstrip().split())) 
  File "<string>", line 1
    5 2
      ^
SyntaxError: unexpected EOF while parsing

My input file is the following:

10
5 2
5 1

The above works when there is only one input() on the tested code. What am I missing for making it work with multiple input() ?

Your code is work for me (with small changes). It works and with capture_output=True , and with capture_output=False

https://drive.google.com/open?id=16NK1_Qfdd98prpmBUqaP894j8uGTNdnf

command to run:

python3 tester.py

For your problem: I suspect in your input files can missing '\n' in some lines (may be editor bug, don't know).

Also try check indents in your module b.py

Can you share your code with input files for detailed analysing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM