Skip to content

Instantly share code, notes, and snippets.

@raresteak
Last active August 17, 2023 15:21
Show Gist options
  • Save raresteak/187a6ce923e838840f915fbfb1d06f67 to your computer and use it in GitHub Desktop.
Save raresteak/187a6ce923e838840f915fbfb1d06f67 to your computer and use it in GitHub Desktop.
Python3 implementation of Unix tree command. Runs on Windows and Nix
import os
# mimic Unix tree command in python
# runs on Windows and Nix
# run from current directory for tree output
def tree(cwd):
print(f"+ {cwd}")
for root, dirs, files in os.walk(cwd):
level = root.replace(cwd, '').count(os.sep)
indent = ' ' * 4 * (level)
print(f"{indent}+ {os.path.basename(root)}/")
sub_indent = ' ' * 4 * (level + 1)
for file in files:
print(f"{sub_indent}- {file}")
if __name__ == '__main__':
tree('.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment