Welcome, guest | Sign In | My Account | Store | Cart

An extremely simple tree-view generator for a directory

Python, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    import os
    import sys


    def create_tree_structure(rootpath):
        rootpath = rootpath.rstrip(os.sep)
        start_level = rootpath.count(os.sep)
        for root, dirs, files in os.walk(rootpath):
            present_level = root.count(os.sep)
            actual_level = present_level - start_level
            spacing = (actual_level) * ' '
            file_list = [file for file in files]
            sys.stdout.write(spacing + '-' + os.path.basename(root) + ' ' +
                             str(file_list) + '\n')

    if __name__ == '__main__':
        create_tree_structure('../path')

I am a python newbie and I am lately learning a lot of things. I had wanted a tree view of my own. I did find some code in the net. But I really wanted to keep things simple. The output generated is quite useful for me, but again, there is room for lot of improvement.

Suggestions,tips, improvements are welkom.

8 comments

Stephen Chappell 12 years ago  # | flag

You might want to check out recipe 577091 to see a close emulator of the Windows TREE command.

Lawrence Porter 12 years ago  # | flag

I'm a python beginner, too, so I assume that I'm doing something wrong. I've tried your recipe, but it doesn't seem to do anything - well, it may do something, but nothing is displayed (or generated). Any chance of an example of how to use it? Thanks.

ajaymenon.k (author) 12 years ago  # | flag

Okay. It should generate the tree view of your directory - the directory pointed to by a valid path that you pass from the main to the function - like '/var/mail/'.

Lawrence Porter 12 years ago  # | flag

Still no luck. But something has occurred to me: are you running Windoze or linux? All I get is a bit of a jumble on the screen; could be the wrong OS. Thanks, though.

ajaymenon.k (author) 12 years ago  # | flag

Linux.

ajaymenon.k (author) 12 years ago  # | flag

Could you please post your output?

Lawrence Porter 12 years ago  # | flag

Here it is:

-Projects ['bind1.py', 'twowins.py', 'hello1.py', 'myDialog.pyc', 'menu1.py', 'tst_curses2.e3p', 'myWindow.py', 'myDialog.py', 'toolbar1.py', 'myBoolean.pyc', 'soduko_prep.py', 'myBoolean.py', 'tst_curses.e3p', 'hello2.py'] -MultiLists ['multi_list_box.py', 'multi_listboxes.e3p'] -docmik_window ['window_06.py', 'window_09.py', 'window_05.py', 'window_01.py', 'window_02.py', 'window_03.py', 'window_07.py', 'window_08.py', 'window_04.py'] -docmik_editor ['findz.py', 'findz.pyc', 'configure_01.py', 'find.py', 'editor_08.py', 'editor_05.py', 'dialogs.pyc', 'files_03.pyc', 'files_02.py', 'replace.gif', 'editor_03.py', 'dialogs_01.py', 'find.pyc', 'editor_01.py', 'editor_04.py', 'next.gif', 'dialogs_01.pyc', 'files_02.pyc', 'paste.gif', 'editor_09.py', 'configure.py', 'editor_09z.py', 'copy.gif', 'editor_07.py', 'open.gif', 'editor_06.py', 'new.gif', 'save.gif', 'dialogs_02.py', 'files_03.py', 'dialogs.py', 'cut.gif', 'saveas.gif', 'dialogs_02.pyc', 'configure.pyc', 'print.gif', 'find.gif', 'files.py', 'header.py', 'configure_01.pyc', 'files.pyc', 'editor.py', 'editor_02.py', 'files_01.py'] -save ['configure_01y.pyc', 'configure_01y.py', 'configure_01x.py'] -dialog_box ['zip_load.py', 'soduko_prp.e3p', 'soduko_prp.e3s', 'zip_load.e3p', 'soduko_prp.py', 'zip_load'] -cookb_20120322 ['dir_tree_view_02.py', 'dir_tree_view.py', 'txt_file_searcher.py', 'splash_screen.py', 'file_names.py', 'BMI-metric.py', 'tree_view.txt', 'path_stats.py'] -TkSudoku ['tstframe.py', 'minimapp3.py', 'tstkip2.py', 'minimapp.py', 'multilabs.py', 'sudoku_ip.py', 'tstquit.py', 'minimapp2.py', 'minimapp7xx.py', 'minimapp7.py', 'tstentry.py', 'minimapp9.py', 'minimapp5.py', 'gen_cell_ident2.py', 'minimapp6.py', 'multilabs2.py', 'tkex1.py', 'minimapp4.py', 'tstkip.py', 'gen_cell_ident.py', 'minimapp8.py'] -Sudoku ['testing2.e3p', 'Sudoku_PrepX.e3p', 'sudoku_prep.py', 'test_curses.py', 'sudoku_kbip.py', 'testing1.py', 'test_curses2.py', 'Sudoku_Kbip.e3p'] -savedstuff ['sudoku_prep.py']

HTH

ajaymenon.k (author) 12 years ago  # | flag

This is the structure I had in mind when I wrote it :

-root dir

  • sub dir 1
  • sub dir 2 [ list of files in sub dir 2 ]
    • sub- sub dir 2 [ list of files in sub sub dir 2]

In that way the output is correct. But i guess you might need to tinker around with the code a bit. Instead of using spaces for the spacing, use some other character to get more clarity.