Last active
August 17, 2023 15:21
-
-
Save raresteak/187a6ce923e838840f915fbfb1d06f67 to your computer and use it in GitHub Desktop.
Python3 implementation of Unix tree command. Runs on Windows and Nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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