Created
August 5, 2024 07:40
-
-
Save sglbl/5dc4267ccc65d4a2a9ec37abc50b95f5 to your computer and use it in GitHub Desktop.
Directory Tree Structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| # Show file tree structure in the current directory | |
| def list_file_structure(startpath): | |
| for root, dirs, files in os.walk(startpath): | |
| # Skip directories that start with a dot, __pycache__, and also include .git explicitly | |
| dirs[:] = [d for d in dirs if not (d.startswith('.') or d == '__pycache__')] | |
| level = root.replace(startpath, '', 1).count(os.sep) | |
| indent = ' ' * 4 * (level) | |
| print('{}{}/'.format(indent, os.path.basename(root))) | |
| subindent = ' ' * 4 * (level + 1) | |
| for f in files: | |
| # Skip files that start with a dot | |
| if not f.startswith('.'): | |
| print('{}{}'.format(subindent, f)) | |
| # Example usage: | |
| list_file_structure(".") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment