import subprocess path = ROOT_DIR result = (subprocess.check_output(['tree', '--dirsfirst', path]) .decode("utf-8", "strict")) file_list = result.split('\n') root = file_list[0] file_list = file_list[1:-3] header_fmt = "{}" file_fmt = """ {} description """* len(file_list) formatted = """ {} {}
""".format( header_fmt.format(root), file_fmt.format(*file_list) ) print(formatted)