簡體   English   中英

從 python 運行另一個 python 程序

[英]Running another python program from python

我正在嘗試構建一個測試腳本來測試我的 python 代碼。

測試代碼應該從標准輸入讀取並寫入標准輸出。

測試器代碼應該調用測試代碼將文件中的值作為標准輸入傳遞,並讀取標准輸出以與預期值進行比較(存儲在另一個文件中)

測試代碼:

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')

測試代碼:

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))

我收到了多個以下錯誤(禁用 capture_output 以更好地可視化時):

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

我的輸入文件如下:

10
5 2
5 1

當測試代碼上只有一個input()時,上述方法有效。 使它與多個input()一起工作我缺少什么?

您的代碼對我有用(稍作改動)。 它適用於capture_output=Truecapture_output=False

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

運行命令:

python3 tester.py

對於您的問題:我懷疑您的輸入文件中的某些行可能缺少 '\n' (可能是編輯器錯誤,不知道)。

還可以嘗試檢查模塊b.py中的縮進

您可以與輸入文件共享您的代碼以進行詳細分析嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM