Directory Tree Printer
This script is designed to print the directory tree of a specified directory. It recursively lists all subdirectories and files, excluding hidden entries (those starting with a dot .
). The output is formatted to clearly visualize the directory structure. Additionally, an alternative method using the seedir
library with emoji styling is included for a more visually engaging output.
Features
- Recursive Listing: Traverses all subdirectories and files.
- Exclusion of Hidden Entries: Hidden directories and files are not included in the scratch method.
- Formatted Output: Uses connectors (
├──
,└──
,│
) or emoji-based styling for a clear visual structure. - Custom Sorting: Prioritizes directories over files in the output.
- Flexible Display Options: Supports both a custom recursive function and the
seedir
library with emoji styling.
How It Works
- Scratch Method:
- Lists all entries in the specified directory.
- Separates directories and files, excluding hidden entries.
- Sorts directories and files, listing directories first.
- Prints entries with appropriate connectors and indentation.
- Recurses into subdirectories with updated indentation and prefix.
- Seedir Method:
- Uses the
seedir
library to generate a directory tree. - Applies emoji styling for visual appeal.
- Custom sorting prioritizes folders over files.
- Limits the number of displayed items per directory for brevity, with an option to indicate additional content.
- Uses the
Example Usage
Scratch Method
import os
def print_directory_tree(directory, indent="", prefix=""):
# Get list of all entries in the directory
entries = os.listdir(directory)
# Separate directories and files, excluding hidden entries
dirs = sorted([e for e in entries if os.path.isdir(os.path.join(directory, e)) and not e.startswith('.')])
files = sorted([e for e in entries if not os.path.isdir(os.path.join(directory, e)) and not e.startswith('.')])
# Combine: directories first, then files
sorted_entries = dirs + files
for index, entry in enumerate(sorted_entries):
path = os.path.join(directory, entry)
# Determine connector based on whether it's the last entry
connector = "└──" if index == len(sorted_entries) - 1 else "├──"
# Print current entry
print(f"{indent}{prefix}{connector} {entry}")
# If it's a directory, recurse with updated indent and prefix
if os.path.isdir(path):
# Use different prefix for last entry to align properly
new_prefix = " " if index == len(sorted_entries) - 1 else "│ "
print_directory_tree(path, indent + new_prefix, "")
# Example usage
directory = "./sample_data" # Specify directory
print(os.path.basename(os.path.abspath(directory))) # Print root directory name
print_directory_tree(directory)
Seedir Method
pip install seedir emoji
from seedir import seedir
import os
# Custom sort key to prioritize folders
def sort_folders_first(item):
return (not os.path.isdir(os.path.join(directory, item)), item.lower())
# Display directory structure with folders sorted first and `emoji` style
directory = "./sample_data" # Specify directory
seedir(directory, itemlimit=3, beyond="content", style='emoji', sort=True, sort_key=sort_folders_first)
Example Output
Scratch Method
sample_data
├── dir1
│ ├── file1.txt
│ ├── file2.txt
│ ├── file3.txt
│ ├── file4.txt
│ └── file5.txt
├── dir2
│ └── file6.txt
└── file7.txt
Seedir Method
sample_data📁
├─📁dir1
│ ├─📄file1.txt
│ ├─📄file2.txt
│ ├─📄file3.txt
│ └─0 folder(s), 2 file(s)
├─📁dir2
│ └─📄file6.txt
└─📄file7.txt
Notes
- The scratch method excludes hidden directories and files by default and uses simple text-based connectors for clarity.
- The
seedir
method enhances the output with emoji icons and allows limiting the number of displayed items per directory (e.g.,itemlimit=3
). - Both methods prioritize directories over files in the output for better organization.
- To use the
seedir
method, ensure theseedir
andemoji
packages are installed (pip install seedir emoji
). - Modify the directory path or parameters (e.g.,
itemlimit
,style
) to suit your needs.
This markdown provides a comprehensive overview of the directory tree printing script, incorporating both the scratch recursive method and the seedir
-based method with emoji styling.