python tree树形显示文件、文件夹 作者:马育民 • 2024-10-29 22:54 • 阅读:10012 ``` ############################################################################### # Copyright (C) 2008 - 2020 NONE, Inc. All rights reserved. ############################################################################### #!/usr/bin/python3 # coding:utf-8 # @author:xxxxxx@qq.com # @time:2020-04-27 09:44:50 # @file:tree_main.py import os import sys # -d 只显示目录 # -a 显示所有文件 # -L num 显示几层 ,不显示隐藏文件 curent_os = sys.platform def print_color(color, argv): if curent_os == "win32": print(argv) return if color == "green": print("\033[32m", argv, "\033[0m") elif color == "red": print("\033[31m", argv, "\033[0m") elif color == "yellow": print("\033[33m", argv, "\033[0m") else: print(argv) def parse_option(op_str): ''' 解析参数 ''' # print("option:" + op) import re layer_limit = False layer_max = 0 direct_only = False show_hidden_file = False # search layer limit match_str = format("-L[\s]+\d") search_ret = re.search(match_str, op_str) if search_ret is not None: # print(search_ret.group(0)) layer_limit = True match_str = format("\d") search_ret = re.search(match_str, search_ret.group(0)) if search_ret is not None: # print(search_ret.group(0)) layer_max = int(search_ret.group(0)) # search hidden cfg match_str = format("-a") search_ret = re.search(match_str, op_str) if search_ret is not None: # print(search_ret.group(0)) show_hidden_file = True # search direct only match_str = format("-d") search_ret = re.search(match_str, op_str) if search_ret is not None: # print(search_ret.group(0)) direct_only = True return show_hidden_file, direct_only, layer_limit, layer_max def print_side(show_sides): ''' 打印 | 或者 空格 ''' for side in show_sides: if side: print("│ " , end="") else: print(" " , end="") def get_files_dirs(dir,show_hidden_file): ''' 传入路径,获取该路径下所有的文件列表,文件夹列表,并且是排序过的 返回值:元组, 第一个元素:文件列表 第二个元素:文件夹列表 ''' file_lst=[] # 存放文件 dir_lst=[] # 存放文件夹 for item in os.scandir(dir): if not show_hidden_file and item.name[0] == '.': continue if item.is_dir(): dir_lst.append(item.name) elif item.is_file(): file_lst.append(item.name) # 排序 file_lst.sort() dir_lst.sort() return file_lst,dir_lst def tree_dir(dir, show_hidden_file, direct_only, layer_limit, layer_max,layer=0,show_sides = [True]): ''' 显示目录 参数: - 查看路径 - 是否隐藏文件 - 是否显示路径 - 是否有层级限制 - 最大层级数量 - show_sides:是否显示 | ''' file_lst,dir_lst = get_files_dirs(dir,show_hidden_file) len_of_listdir = len(dir_lst) len_of_listfile = len(file_lst) # 显示文件夹 for index,file in enumerate( dir_lst ): file_path = os.path.join(dir, file) if layer_limit == True and layer >= layer_max: continue print_side(show_sides) # 继承上级的 show_sides ,准备填充本级目录是否显示 | curr_show_sides = [] curr_show_sides.extend(show_sides) if layer >= 0: if index == len_of_listdir-1: # 显示最后一个路径 if direct_only is not True and len_of_listfile>0: print("├ ", end="") curr_show_sides.append(True) else: print("└ ", end="") curr_show_sides.append(False) else: print("├ ", end="") curr_show_sides.append(True) print_color("green", file) tree_dir(file_path, show_hidden_file, direct_only, layer_limit, layer_max,layer + 1,curr_show_sides) # 如果显示文件 if direct_only is not True: for index,file in enumerate( file_lst): file_path = os.path.join(dir, file) if layer_limit == True and layer >= layer_max: continue print_side(show_sides) if layer >= 0: if index == len_of_listfile and len_of_listdir == 0: print("└ ", end="") # 显示最后一个文件 else: if index == len_of_listfile-1: print("└ ", end="") # 显示最后一个文件 else: print("├ ", end="") print(file) if __name__ == '__main__': file_name = sys.argv[0] if len(sys.argv) < 2: print_color("red", "参数错误!\n") print("请输入参数:") print(" 路径,当前路径为 .") print(" -d 只显示目录") print(" -a 显示所有文件(.开头的文件)") print(" -L num 显示几层 ,不显示隐藏文件\n") sys.exit(1) path_dir = sys.argv[1] op = "" for idx in range(len(sys.argv)): if idx >= 2: op += " " + sys.argv[idx] show_hidden_file, direct_only, layer_limit, layer_max = parse_option(op) print(show_hidden_file, direct_only, layer_limit, layer_max) print() print(path_dir) tree_dir(path_dir,show_hidden_file, direct_only, layer_limit, layer_max,0,[]) sys.exit(0) ``` 原文出处:http://malaoshi.top/show_1IX8hFhJjWMt.html