
Python编写文件树生成工具
发布日期:2023-05-26 13:19:30
浏览次数:2
分类:技术文章
本文共 5626 字,大约阅读时间需要 18 分钟。
使用Python编写文件树生成工具
编写Python代码
代码部分编写较为复杂,请自行解读
import osimport sysclass 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)})''')
打包成exe工具
下载pyinstaller
pip install pyinstaller
打包
pyinstaller -F -w getTree.py
输入以上命令实现打包
在当前目录下会自动生成一个dist
文件夹,文件夹中就有一个getTree.exe
可执行脚本
编辑注册表
按住windows + R
输入regedit
进入注册表
进入到以下目录:计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell
在shell
下新建项GetTree
在GetTree
下新建项command
修改command
的值为
"D:\Program Files\GetFileTree\getTree.exe" "%v."
此时右键文件夹空白处就会有一个GetTree
菜单,选中菜单即可运行程序
演示使用效果
cmd命令行
查看所有文件树
输入以下命令,默认查看当前目录文件树,在当前目录下生成一个fileTree.md
文件
getTree
查看某个目录的文件树
getTree "D:\学习笔记\Git\OutputFileTree\123123"
查看文件夹树
查看文件夹树,可在最后面加上-d
参数
getTree "D:\学习笔记\Git\OutputFileTree\123123" -d
菜单右键生成当前目录的文件树

转载地址:https://blog.csdn.net/m0_43402033/article/details/129823796 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2023年06月11日 02时21分44秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
7-4 回文判断 (20分)
2019-12-15 09:51:03
6-1 计算两数的和与差 (10分)
2019-12-15 09:51:03
6-2 交换两个整数的值 (10分)
2019-12-15 09:51:04
7-5 求a[10]中素数平均值 (10分)
2019-12-15 09:51:01
7-3 将数组中的数逆序存放 (20分)
2019-12-15 09:51:01
7-4 矩阵转置 (10分)
2019-12-15 09:51:01
7-5 一个数插入到有序数列中 (10分)
2019-12-15 09:51:02
7-7 以矩阵的形式输出二维数组 (15分)
2019-12-15 09:51:02
7-8 求矩阵的最大值(设惟一) (15分)
2019-12-15 09:51:02
7-2 冒泡法排序之过程 (15分)
2019-12-15 09:51:02
6-3 递归求阶乘和 (15分)
2019-12-15 09:51:00
6-4 递归计算Ackermenn函数 (15分)
2019-12-15 09:51:00
6-5 递归计算P函数 (15分)
2019-12-15 09:51:00
7-1 求最大值及其下标 (20分)
2019-12-15 09:51:00
7-2 查找整数 (10分)
2019-12-15 09:51:01
7-3 交换最小值和最大值 (15分)
2019-12-15 09:51:01
7-4 输出数组元素 (15分)
2019-12-15 09:51:01
6-4 使用函数求最大公约数 (10分)
2019-12-15 09:50:58
6-5 大于m的最小素数 (10分)
2019-12-15 09:50:59
6-6 求一个整数各位数字平方和 (10分)
2019-12-15 09:50:59