|
| 1 | +import os |
| 2 | +import argparse |
| 3 | + |
| 4 | + |
| 5 | +# function to generate a pattern |
| 6 | +def get_pattern(level: int) -> str: |
| 7 | + # getting the number of spaces required |
| 8 | + spaces = 2 * (level + 1) |
| 9 | + |
| 10 | + # getting the number of separators required |
| 11 | + separators_length = spaces // 2 |
| 12 | + |
| 13 | + # generating the pattern |
| 14 | + pattern = '' |
| 15 | + |
| 16 | + for i in range(separators_length): |
| 17 | + pattern += ' |' |
| 18 | + |
| 19 | + # adding the finishing |
| 20 | + pattern += '__' |
| 21 | + |
| 22 | + return pattern |
| 23 | + |
| 24 | + |
| 25 | +# function to print the structure |
| 26 | +def print_structure(path: str, level: int) -> None: |
| 27 | + # printing the dir name |
| 28 | + if level == 0: # if it is the root directory then print the name as it is |
| 29 | + print(path) |
| 30 | + else: # if it is not the root directory |
| 31 | + # if dir name is something like dir/indir/then make it indir/ |
| 32 | + if '/' in path: |
| 33 | + # the header information should start with level - 1 |
| 34 | + print(f'{get_pattern(level - 1)}{path.split("/")[-1]}/') |
| 35 | + else: |
| 36 | + # if dir name is dir then show the user dir/ |
| 37 | + # so that it is clear that it is a directory |
| 38 | + print(f'{get_pattern(level)}{path}/') |
| 39 | + |
| 40 | + # list the contents of the dir |
| 41 | + contents = os.listdir(path) |
| 42 | + |
| 43 | + # iterating through the each content |
| 44 | + for content in contents: |
| 45 | + # joining the path and the content name |
| 46 | + full_path = os.path.join(path, content) |
| 47 | + |
| 48 | + # checking if the content is the file |
| 49 | + if os.path.isfile(full_path): |
| 50 | + # if it is then print the file name |
| 51 | + print(f'{get_pattern(level)}{content}') |
| 52 | + else: # if it is a dir instead |
| 53 | + # add spacing vertically b/w different folders if requested |
| 54 | + if args.spacing: |
| 55 | + print(get_pattern(level)[:-2]) |
| 56 | + |
| 57 | + # call the print_structure function again with the new path |
| 58 | + print_structure(full_path, level + 1) |
| 59 | + |
| 60 | + # return if all the content of the folder has been iterated |
| 61 | + return |
| 62 | + |
| 63 | + |
| 64 | +# the main function |
| 65 | +def main(args: argparse.Namespace) -> None: |
| 66 | + # simplifying the arguments |
| 67 | + dir_path = args.directory |
| 68 | + |
| 69 | + # if the passed directory name is invalid then inform the user and exit |
| 70 | + if not os.path.isdir(dir_path): |
| 71 | + print('ERROR: can not find the path specified!') |
| 72 | + exit(0) |
| 73 | + |
| 74 | + # printing the structure for the given path and the level |
| 75 | + print_structure(dir_path, 0) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + # creating the argparse object |
| 80 | + parser = argparse.ArgumentParser() |
| 81 | + |
| 82 | + # creating an argument for the directory |
| 83 | + parser.add_argument( |
| 84 | + '-d', '--directory', |
| 85 | + metavar='', |
| 86 | + help='path to the directory. [DEFAULT=\'.\']', |
| 87 | + type=str, default='./' |
| 88 | + ) |
| 89 | + |
| 90 | + # adding argument for vertical spacing b/w folders |
| 91 | + parser.add_argument( |
| 92 | + '-s', '--spacing', |
| 93 | + action='store_true', |
| 94 | + help='Add spacing b/w folders output vertically' |
| 95 | + ) |
| 96 | + |
| 97 | + # parsing the arguments |
| 98 | + args = parser.parse_args() |
| 99 | + |
| 100 | + # passing the args to the main method |
| 101 | + main(args) |
0 commit comments