1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| import os import sys
class GetDirTree: def __init__(self, directory='.', only_dir=False): self.directory = os.path._getfullpathname(directory) self.part_directory = '\\'.join(self.directory.split('\\')[:-1]) self.path = [self.directory.split('\\')[-1]] self.only_dir = only_dir self.path_num = {}
def __loop(self, dir_): tmp_path = self.listdir(dir_) for i in tmp_path: tmp = os.path.join(dir_, i) self.path.append(tmp.replace(self.part_directory + '\\', '')) if os.path.isdir(tmp): self.__loop(tmp)
def __get_num(self): for i in self.path: self.path_num[i] = len(i.split('\\'))
def run(self): self.__loop(self.directory) self.__get_num() self.plot_tree() return self.path_num
def listdir(self, dir_): tmp = os.listdir(dir_) all_dir = [i for i in tmp if os.path.isdir(os.path.join(dir_, i)) and i[0] != '.'] all_file = [i for i in tmp if not os.path.isdir(os.path.join(dir_, i)) and i[0] != '.'] if self.only_dir: return all_dir else: return all_file + all_dir
@staticmethod def __get_special(list_): res = {} res2 = [] for i in range(len(list_)): if i == len(list_) - 1: tmp_index = len(list_) - 1 tmp = list_[tmp_index] while True: if list_[tmp_index] == tmp: res[tmp_index] = 1 tmp_index -= 1 else: res2.append(tmp_index) break elif list_[i] - list_[i + 1] > 1: tmp_index = i tmp = list_[i] while True: if list_[tmp_index] == tmp: res[tmp_index] = list_[i] - list_[i + 1] + 1 tmp_index -= 1 else: res2.append(tmp_index) break return res, res2
def plot_tree(self, filename='fileTree.md'): if os.path.exists(filename): os.remove(filename) print(f'Total {len(self.path_num)} files.') names = list(self.path_num.keys()) nums = list(self.path_num.values()) special_num, special_num2 = self.__get_special(nums) strings = [] strings.append(names[0].split('\\')[-1] + '\n') for i in range(1, len(nums)): end_string = names[i].split('\\')[-1] + '\n' if i == len(nums) - 1 or (nums[i - 1] <= nums[i] and nums[i + 1] < nums[i]): string = ' │ ' * (nums[i] - 2) + ' └─' + end_string else: string = ' │ ' * (nums[i] - 2) + ' ├─' + end_string if i in special_num.keys(): string = string[::-1].replace('│', ' ', special_num[i] - 2)[::-1] if i in special_num2: string = string.replace('├─', '└─') strings.append(string) for index in list(range(len(strings)))[::-1]: for n in range(max(nums)): check_index = 2 + n * 4 try: if strings[index][check_index] == ' ' and strings[index - 1][check_index] == '│': tmp_list = list(strings[index - 1]) tmp_list[check_index] = ' ' strings[index - 1] = ''.join(tmp_list) elif strings[index][check_index] == ' ' and strings[index - 1][check_index] == '├': tmp_list = list(strings[index - 1]) tmp_list[check_index] = '└' tmp_list[check_index + 1] = '─' strings[index - 1] = ''.join(tmp_list) elif strings[index][check_index] == '├' and strings[index + 1][check_index] not in ['├', ' ', '│', '└']: tmp_list = list(strings[index]) tmp_list[check_index] = '└' strings[index] = ''.join(tmp_list) except: ... [print(str_)for str_ in strings] with open(filename, 'a', encoding='utf-8') as f: f.writelines(strings)
if __name__ == '__main__': input_ = sys.argv if len(input_) == 1: get_dir = GetDirTree() get_dir.run() elif len(input_) == 2: get_dir = GetDirTree(sys.argv[1]) get_dir.run() elif len(input_) == 3: if '-d' in sys.argv: sys.argv.remove('-d') get_dir = GetDirTree(sys.argv[1], only_dir=True) get_dir.run() else: print(f'''Input Error, if you only want to get directory tree. Please use (python *.py d directory)! Not (python {" ".join(sys.argv)})''')
|