Skip to content

Instantly share code, notes, and snippets.

@pakkinlau
Created December 23, 2023 10:47
Show Gist options
  • Save pakkinlau/fe7ed183e6552999f6fd74aa258802e0 to your computer and use it in GitHub Desktop.
Save pakkinlau/fe7ed183e6552999f6fd74aa258802e0 to your computer and use it in GitHub Desktop.
recursively prints text that contain a tree structure representing the folders and files in a given directory. It provides insights to LLM or human developer into the structure of a package.
import os
def print_tree(directory, indent=''):
files = os.listdir(directory)
files.sort() # Sort files alphabetically
for file in files:
path = os.path.join(directory, file)
if os.path.isfile(path):
# Print file name with proper indentation
print(f"{indent}|-- {file}")
elif os.path.isdir(path):
# Print directory name with proper indentation
print(f"{indent}|-- {file}/")
print_tree(path, indent + "| ")
# Example usage
directory_path = r"C:\Users\kinla\Documents\All_github_repo\gites\gites" # Replace with your desired directory path
print_tree(directory_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment