Skip to content

Instantly share code, notes, and snippets.

@rreemmii-dev
Created August 1, 2022 20:54
Show Gist options
  • Save rreemmii-dev/e96188dd8c0889247a79680d7c50c6a5 to your computer and use it in GitHub Desktop.
Save rreemmii-dev/e96188dd8c0889247a79680d7c50c6a5 to your computer and use it in GitHub Desktop.

Directory Tree Generator

This little program allows you to generate a directory tree. The output will be like that:

├─ css/
|  ├─ contact/
|  |  ├─ contact.css
|  |  ├─ contact.css.map
|  |  └─ contact.scss
|  ├─ index/
|  |  ├─ index.css
|  |  ├─ index.css.map
|  |  └─ index.scss
├─ images/
|  ├─ background.png
|  └─ top.png
├─ js/
|  ├─ index.js
|  └─ jquery.min.js
├─ .htaccess
├─ contact.html
└─ index.html
from os import listdir
from os.path import isdir, join
path = "The ABSOLUTE path of your directory"
ignore = [".git", ".gitignore", ".idea", "__pycache__"] # The files and folders with these names will be ignored
def generate_directory_tree(path, indents=0, text=""):
dir_content = sorted([f for f in listdir(path) if f not in ignore], key=lambda f: str(not isdir(join(path, f))) + f)
for element in dir_content:
if isdir(join(path, element)):
text += f"\n{'| ' * indents}├─ {element}/"
text = generate_directory_tree(join(path, element), indents + 1, text)
elif element == dir_content[-1]:
text += f"\n{'| ' * indents}└─ {element}"
else:
text += f"\n{'| ' * indents}├─ {element}"
return text
print(generate_directory_tree(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment