Created
December 12, 2019 22:59
-
-
Save zperk13/8cc528c32c9d18476ab2ed2153ae32f7 to your computer and use it in GitHub Desktop.
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 | |
from os.path import isdir | |
indent_amount = 4 | |
collapse_amount = 10 | |
indent_char = ' ' | |
# indent_amount = 1 | |
# collapse_amount = 5 | |
# indent_char = '-' | |
indent_char *= indent_amount | |
def print_dir(path, _indent=0, dir_color=False): | |
assert isdir(path), path | |
if path in ['.', '.\\', './', '']: | |
path = os.getcwd() | |
if dir_color: | |
print('\u001b[31m', end='') | |
print(f'{indent_char * _indent}{path.split("/")[-1]}') | |
if dir_color: | |
print('\u001b[0m', end='') | |
_indent += 1 | |
try: | |
listdir_result = list(os.listdir(path)) | |
length = len([x for x in listdir_result if not isdir(f'{path}/{x}')]) | |
print_item_amount = length > collapse_amount | |
if print_item_amount: | |
print(f'{indent_char * _indent}{length} items') | |
for x in listdir_result: | |
full_path = f'{path}/{x}' | |
if not isdir(full_path): | |
if not print_item_amount: | |
print(indent_char * _indent + x) | |
else: | |
print_dir(full_path, _indent=_indent, dir_color=dir_color) | |
except PermissionError: | |
print(f'{indent_char*3}PERMISSION DENIED') | |
print_dir('./', dir_color=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment