简体   繁体   中英

python directory and file list - tree structure ouput

Is it possible using Python (or some kind of lib) to generate an ascii based tree structure of a directory and all its subdirectories + files?

I've tried a bunch of thing but unfortunately I have not been able to solve this problem.

An example of the output would look something like this:

[rootdir]
|
+--- [subdir0]
|
+--- [subdir1]
|     |
|     +--- file1
|     +--- file2
|
+--- [subdir2]
|     |
|     +--- [subdir3]
|     |
|     +--- [subdir4]
|           |
|           +--- [subdir5]
|                 |
|                 +--- [subdir6]
|                 |     |
|                 |     +--- file4
|                 |
|                 +--- file3
+--- file5
+--- file6

Edit:

My current (aweful) script was requested.

def treeStructure(startpath):

    for root, dirs, files in os.walk(startpath):
        level = root.replace(startpath, '').count(os.sep)
        indent = ' ' * 2 * (level)
        print('{}|'.format(indent[:]))
        print('{}+{}/'.format(indent, os.path.basename(root)))
        subindent = ' ' * 2 * (level + 1)

        for f in files:
            print('{}| +--- {}'.format(subindent[:-2], f))

There is a utility called tree on Linux which already does this.

You can also checkout this Python snippet for generating ASCII trees.

I know it's

Shitty Programming

code but it works. ;)

Here you:

import os

# start point
startpath = '.'

folders = []    # folder store
tuples = []     # folders, subdirs, files


def folder_tree(line, dir):
    one = '|-> V '
    padding = '|   '

    if line == dir:
        # print('V '+line)
        return ('V '+line)

    if line.count(os.sep) == 1:
        line = line.split(os.sep)
        line[0] = one
        # print(''.join(line))
        return (''.join(line))

    if line.count(os.sep) >= 2:
        line = line.split(os.sep)
        line[-2] = one
        for i in range(len(line[:-2])):
            line[i] = padding
        # print(''.join(line))
        return (''.join(line))


def files_tree(dir, *args):
    """

    :param dir: startpath
    :param args: args[0] > tuples, args[1] > folders
    :return: None
    """
    file = '|-> '
    padding = '|   '
    last_file = ''
    tuples = args[0]
    folders_list = args[1]
    for root, subs, files in tuples:
        # no files no worries, skip
        if not files:
            continue

        # will use for padding: padding * sep
        sep = root.count(os.sep)

        # only if root has some files
        if root == dir:
            last_file = [file+str(x) for x in files]
            continue

        if subs:
            # take last elem in subs,
            # use it as value to find the same in folders_list
            # get index + 1 to insert right after
            index = folders_list.index([x for x in folders_list if x.endswith(subs[-1])][0]) + 1

        else:
            # we need name the last of folder in the root
            # to use it to find index
            folder_name = root.split(os.sep)[-1]
            index = folders_list.index([x for x in folders_list if x.endswith(folder_name)][0]) + 1

        # prepare files
        files = [sep * padding + file + x for x in files]

        # now insert files to list
        for i, a in enumerate(range(index, index+len(files))):
            folders_list.insert(a, files[i])

    if last_file:
        # merge files in root dir
        folders_list = folders_list + last_file

    # final print tree
    for elm in folders_list:
        print(elm)


def tree_walk(dir):
    for folder, subs, files in os.walk(dir):
        tuples.append((folder, subs, files))
        folders.append(folder_tree(folder, dir))


tree_walk(startpath)
folder_tree(tuples, startpath)
files_tree(startpath, tuples, folders)

And here is result:

V .
|-> V folder1
|   |-> V folder2
|   |   |-> V folder3
|   |   |   |-> file3.txt
|   |   |-> file2.txt
|   |-> V folderX
|   |-> file1.txt
|-> 02-hw1_wdwwfm.py
|-> 06-t1-home1.py
|-> 06-t1-home2.py
|-> hw1.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM