Skip to content

Instantly share code, notes, and snippets.

@klange
Created April 2, 2017 01:48
Show Gist options
  • Save klange/f5c1c95fddce9fd21a51535ebea9d0a6 to your computer and use it in GitHub Desktop.
Save klange/f5c1c95fddce9fd21a51535ebea9d0a6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""
tree.py - List directories in a visual tree.
"""
import os
def print_directory(path, prefix="", last=False):
if last:
extra = "└─"
else:
extra = "├─"
print(f"{prefix}{extra}{os.path.basename(path)}")
if not last:
prefix += "│ "
else:
prefix += " "
if os.path.isdir(path):
subs = os.listdir(path)
for i in range(len(subs)):
p = os.path.join(path,subs[i])
if i == len(subs)-1:
print_directory(p,f"{prefix}",True)
else:
print_directory(p,f"{prefix}",False)
print_directory(".",last=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment