Makistos
2/1/2017 - 2:34 PM

Read files recursively. Useful for e.g. command files. #python #file-handling

Read files recursively. Useful for e.g. command files. #python #file-handling

def parse_test_file(filename):
    retval = []
    with open(filename) as f:
        lines = f.read().splitlines()

    path, script = os.path.split(os.path.abspath(filename))
    cwd = os.getcwd()
    if cwd != path:
        os.chdir(path)
    for line in lines:
        if line.startswith('/include'):
            retval = retval + parse_test_file(line[9:])
        else:
            retval.append(line)
    if cwd != path:
        os.chdir(cwd)

    return retval