A small Python CLI utility to print a directory tree, with support for:
config.json├── / └── connectorsconfig.jsonpyperclipInstall dependencies:
pip install pyperclip
Save the script as, for example:
tree.py
(Optional) Make it executable on Unix systems:
chmod +x tree.py
python tree.py [path] [options]
If no path is provided, the current directory (.) is used.
| Flag | Description |
|---|---|
-l, --level <n> |
Maximum directory depth |
-c, --clipboard |
Copy output to clipboard instead of printing |
-s, --show-ignore |
Print loaded ignore configuration and exit |
python tree.py
python tree.py /path/to/project
python tree.py -l 2
python tree.py -c
python tree.py --show-ignore
project/
├── README.md
├── src/
│ ├── main.py
│ └── utils.py
└── tests/
└── test_main.py
config.json)The tool looks for a config.json file in the current working directory.
If the file does not exist, defaults are used.
{
"ignore": {
"folders": [],
"files": []
}
}
{
"ignore": {
"folders": ["__pycache__", ".git", "node_modules"],
"files": [".DS_Store", "*.log"]
}
}
Note: Ignore matching is based on exact names only (no glob or wildcard support).
ignore.foldersignore.filesconfig.json → silently ignoredconfig.json → prints error message[Permission denied]load_config()
Loads ignore settings from config.json
should_ignore(path)
Determines whether a file or folder should be skipped
build_tree()
Recursively constructs the tree structure
generate_tree()
Entry point for producing the final tree output
main()
CLI argument parsing and execution logic
MIT (or specify your preferred license)
pyperclipHappy hacking 🌲
```