运行python代码时出现无法识别的参数错误

时间:2018-11-09 05:31:42

标签: python parsing arguments

    import os
import subprocess
import re
import argparse

apps_dir = None
inputs_dir = None
outputs_dir = None

parser = argparse.ArgumentParser(description='Runs Arthur routing test cases.')
parser.add_argument('test_dir', type=str, help='Path to root folder where bin and test_cases folders should exist.')

def run_test(app, test):
    app_exe = os.path.join(apps_dir, app)
    app_ver = re.search(r'ArthurCLI-(d/).exe', app).group(1)
    inputs = os.path.join(inputs_dir, test, app_ver)
    outputs = os.path.join(outputs_dir, test, app_ver)

    if not os.path.exists(inputs):
        return

    if not os.path.exists(outputs):
        os.makedirs(outputs)

    for i in os.listdir(inputs):
        base_fname = os.path.splitext(i)[0]
        i_fname = os.path.join(inputs, i)
        i_file = open(i_fname)

        o_fname = os.path.join(outputs, '%s.out' % (base_fname))
        o_file = open(o_fname, 'w')

        print('%s < %s > %s\n' % (app_exe, i_fname, o_fname))
        p = subprocess.Popen(app_exe, stdin=i_file, stdout=o_file)
        p.wait(),
        o_file.flush()

        i_file.close()
        o_file.close()

if __name__ == '__main__':
    args = parser.parse_args()

    test_dir = os.path.normpath(args.test_dir)
    apps_dir = os.path.join(test_dir, 'bin')
    inputs_dir = os.path.join(test_dir, 'test_cases')
    outputs_dir = os.path.join(test_dir, 'results')
    if not os.path.exists(outputs_dir):
        os.makedirs(outputs_dir)

    for app in os.listdir(apps_dir):
        for test in os.listdir(inputs_dir):
            run_test(app, test)

    print('[Done]')

当我运行此代码时,它将引发错误“无法识别的错误”,因此我想知道在哪里传递目录路径来处理此错误。它需要test_dir的路径才能运行此代码,我该如何处理。我认为主要错误在于主要方法。

0 个答案:

没有答案