list folder structure

import os

def list_files(startpath):
    for root, dirs, files in os.walk(startpath):
        level = root.replace(startpath, '').count(os.sep)
        indent = ' ' * 4 * (level)
        print('{}{}/'.format(indent, os.path.basename(root)))
        subindent = ' ' * 4 * (level + 1)
        for f in files:
            print('{}{}'.format(subindent, f))

Reference:
[1] https://stackoverflow.com/questions/9727673/list-directory-tree-structure-in-python

posted @ 2023-03-21 15:10  xiaoxuxli  阅读(9)  评论(0编辑  收藏  举报