Skip to content

Instantly share code, notes, and snippets.

@sglbl
Created August 5, 2024 07:40
Show Gist options
  • Select an option

  • Save sglbl/5dc4267ccc65d4a2a9ec37abc50b95f5 to your computer and use it in GitHub Desktop.

Select an option

Save sglbl/5dc4267ccc65d4a2a9ec37abc50b95f5 to your computer and use it in GitHub Desktop.
Directory Tree Structure
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