A Neat Way to Visualize Project Structures

Emily Techscribe Avatar

·

Have you ever wanted to view the structure of your project or working directory in a neat and organized way? Look no further, because the Directory Tree package is here to help! With this simple utility package in Python, you can easily display the tree structure of any directory you choose.

About the Directory Tree Package

The Directory Tree package is a Python utility that provides a straightforward way to display the tree structure of a user-defined directory. Whether you want to visualize your project’s structure or explore the contents of a specific directory, this package has got you covered. It is currently available for all platforms, making it accessible to users across different operating systems.

Installation

Installing the Directory Tree package is simple. You have two options:

  1. Install using pip:
    pip install directory_tree
    or
    pip3 install directory_tree

  2. Clone the repository and install:
    git clone https://github.com/rahulbordoloi/Directory-Tree/
    cd Directory-Tree
    pip install -e .

Usage

To display the tree structure of a directory, use the display_tree() function. Here is the function signature and its arguments:

python
display_tree(dir_path: str = '', string_rep: bool=False, header: bool=False, max_depth: float=float("inf"), show_hidden: bool=False, ignore_list: list=None)

  • dir_path: Root path of operation. By default, it refers to the current working directory.
  • string_rep: Boolean flag for direct console output or a string return. By default, it gives console output.
  • header: Boolean flag for displaying operating system and directory path info in the console. Not applicable if string_rep=True.
  • max_depth: Maximum depth of the directory tree. By default, it goes up to the deepest directory or file.
  • show_hidden: Boolean flag for returning/displaying hidden files/directories if set to True.
  • ignore_list: List of file and directory names or patterns to ignore.

To get started, import the necessary libraries and use the display_tree() function:

“`python
from directory_tree import display_tree

if name == ‘main‘:
display_tree(DirectoryPath)
“`

By default, the DirectoryPath is the current working directory unless specified by the user.

Output Examples

Here are a few examples of the output generated by the Directory Tree package:

  1. Displaying the current working directory with header information disabled:
    python
    from directory_tree import display_tree
    display_tree(header=True)

    CWDwithHeader.png

  2. Displaying a user-specified directory with string representation and showing hidden entities:
    python
    from directory_tree import display_tree
    customPath = 'D:\Work\Python Packages Maintainence\Directory-Tree\Test\Main Directory'
    stringRepresentation = display_tree(customPath, string_rep=True, show_hidden=True)
    print(stringRepresentation)

    UserSpecifiedDirectoryStrRepShowHidden.png

  3. Displaying the current working directory with a maximum depth limit of 2:
    python
    from directory_tree import display_tree
    display_tree(max_depth=2)

    UserSpecifiedDirectoryMaxDep.png

Developing Directory Tree

If you’re interested in contributing to the Directory Tree package or exploring its codebase, you can install the development dependencies by running the following command in your virtual environment:

bash
pip install -e .[dev]

Security & Probable Bugs

The Directory Tree package utilizes recursion, which may result in a RecursionError on deep directory trees. However, it should behave well on wide directory trees as the tree is lazily evaluated. Please note that immediate children of a given directory are not lazily evaluated. Additionally, Windows users are advised to use \\ instead of \ in the address to avoid potential issues with escape sequences.

Contact the Author

The Directory Tree package was made with love in Python!

Source: https://github.com/rahulbordoloi/Directory-Tree

Leave a Reply

Your email address will not be published. Required fields are marked *