Takes a folder path and outputs a text representation of the folders and files, supports ignore files.
Project description
Folder Tree Generator
This script generates a tree representation of a folder's structure, including subfolders and files. It also takes into account .gitignore
files to exclude ignored files and folders from the generated tree.
Why?
I needed a way to generate folder structures in a standard text format that I could copy and paste into GPT without including all the build artifacts, eg. repository structures for code analysis. If you wanted to make your own ignore file it should be a simple adapation of a gitignore file, in 90% of my use cases, the gitignore is sufficient.
Usage
To use the script, run the following command:
python3 main.py <root_folder>
Replace <root_folder>
with the path to the folder you want to generate the tree for.
Example
Suppose you have the following folder structure:
my_project/
|-- .gitignore
|-- main.py
|-- utils.py
|-- data/
| |-- input.txt
| |-- output.txt
And the .gitignore
file contains:
data/output.txt
Running the script with python3 main.py my_project
will output the following tree:
my_project/
|-- .gitignore
|-- main.py
|-- utils.py
|-- data/
| |-- input.txt
As you can see, the data/output.txt
file is excluded from the tree because it's listed in the .gitignore
file.
Functions
The script contains the following functions:
parse_gitignore(gitignore_path: Path) -> List[str]
: Parses a.gitignore
file and returns a list of ignored patterns.should_ignore(entry: Path, ignored_patterns: List[str]) -> bool
: Checks if a file or folder should be ignored based on the list of ignored patterns.generate_tree(root: Path, indent: str = "", ignored_patterns: Optional[List[str]] = None) -> str
: Generates a tree of a folder, taking into account the ignored patterns.main(root_folder: str) -> None
: The main function that takes the root folder as an argument and prints the generated tree.
Requirements
- Python 3.6 or higher
- The
pathlib
library (included in the Python standard library since version 3.4)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for folder_tree_generator-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09af0e980e7ec9f44f4e80f355c4121807f41407c1ec84a33fbc8686e2eb96c2 |
|
MD5 | 240018293810ba6bdcea116bd50f97db |
|
BLAKE2b-256 | 81fd26cd7a8acdabcd48105e3e866ef873137ddd0b6962e874dfaaad43410cbb |
Hashes for folder_tree_generator-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4724a941c0b49bb7e905671e7179317810f859e07994563b62eee33af77da98e |
|
MD5 | 6af4abfb0afe573fde477be677cd042e |
|
BLAKE2b-256 | 27ed9b1519cc211d87ae03aaecf1219160c89c5adf9a43928219c8f2baa54fa1 |