Python实现优雅的目录结构打印工具
Python实现优雅的目录结构打印工具
在软件开发、系统管理和日常工作中,我们经常需要查看和分析目录结构。
工具功能概述
这个DirectoryPrinter
类提供了以下功能:
- 递归打印目录结构
- 可配置是否显示隐藏文件
- 可设置最大递归深度
- 自定义缩进和文件/文件夹符号
- 友好的交互式配置界面
核心代码解析
初始化配置
def __init__(self, root_dir, show_hidden=False, depth=0, max_depth=None, indent_symbol='│ ', folder_symbol='/ ', file_symbol='- '):self.root_dir = root_dirself.show_hidden = show_hiddenself.max_depth = max_depthself.indent_symbol = indent_symbolself.folder_symbol = folder_symbolself.file_symbol = file_symbolself.depth = depth
构造函数接收多个参数,允许用户自定义打印行为。特别是indent_symbol
、folder_symbol
和file_symbol
参数,可以完全改变输出样式。
目录打印逻辑
def print_directory(self):try:items = os.listdir(self.root_dir)except FileNotFoundError:print(f"指定的目录 {self.root_dir} 不存在")returnexcept PermissionError:print(f"无法访问目录 {self.root_dir}")return# 过滤隐藏文件if not self.show_hidden:items = [item for item in items if not (item.startswith('.') and os.path.isfile(os.path.join(self.root_dir, item)))]
方法首先尝试列出目录内容,并处理可能出现的异常。然后根据配置过滤隐藏文件。
递归打印
for index, item in enumerate(sorted(items), start=1):path = os.path.join(self.root_dir, item)is_dir = os.path.isdir(path)# 构建前缀prefix = self.indent_symbol * self.depthif index == len(items):if is_dir:print(f"{prefix}└── {self.folder_symbol}{item}")else:print(f"{prefix}└── {self.file_symbol}{item}")else:if is_dir:print(f"{prefix}├── {self.folder_symbol}{item}")else:print(f"{prefix}├── {self.file_symbol}{item}")# 递归处理子目录if is_dir:child_printer = DirectoryPrinter(root_dir=path,show_hidden=self.show_hidden,depth=self.depth + 1,max_depth=self.max_depth,indent_symbol=self.indent_symbol,folder_symbol=self.folder_symbol,file_symbol=self.file_symbol)child_printer.print_directory()
这部分代码实现了递归打印的核心逻辑,使用不同的符号区分文件和文件夹,以及区分是否是最后一项。
使用示例
通过交互式界面配置打印参数:
def main():root_directory = input("请输入要打印的目录路径: ") or os.getcwd()hide_hidden_files = input("是否隐藏隐藏文件? (y/n): ").lower() != 'y'max_depth = input("请输入最大递归深度 (留空表示无限制): ").strip() or Noneauto_print = input("是否自动打印目录结构? (y/n): ").lower() == 'y'printer = DirectoryPrinter(root_dir=root_directory,show_hidden=hide_hidden_files,max_depth=max_depth)printer.print_directory()
输出效果
示例输出可能如下:
├── / dir1
│ ├── - file1.txt
│ └── / subdir
│ └── - file2.txt
└── / dir2├── - file3.txt└── - file4.txt
附录
import osclass DirectoryPrinter:def __init__(self, root_dir, show_hidden=False, depth=0, max_depth=None, indent_symbol='│ ', folder_symbol='/ ', file_symbol='- '):"""初始化目录打印器:param root_dir: 指定要打印的目录路径:param show_hidden: 是否打印隐藏文件和文件夹(默认为 False):param depth: 当前递归深度(内部使用,用户无需设置):param max_depth: 最大递归深度,None 表示无限制:param indent_symbol: 缩进符号:param folder_symbol: 文件夹前缀符号:param file_symbol: 文件前缀符号"""self.root_dir = root_dirself.show_hidden = show_hiddenself.max_depth = max_depthself.indent_symbol = indent_symbolself.folder_symbol = folder_symbolself.file_symbol = file_symbolself.depth = depthdef print_directory(self):"""打印目录结构"""try:items = os.listdir(self.root_dir)except FileNotFoundError:print(f"指定的目录 {self.root_dir} 不存在")returnexcept PermissionError:print(f"无法访问目录 {self.root_dir}")return# 过滤隐藏文件if not self.show_hidden:items = [item for item in items if not (item.startswith('.') and os.path.isfile(os.path.join(self.root_dir, item)))]# 递归打印目录和文件for index, item in enumerate(sorted(items), start=1):path = os.path.join(self.root_dir, item)is_dir = os.path.isdir(path)# 根据当前深度和最大深度决定是否继续递归if self.max_depth is not None and self.depth > self.max_depth:continue# 构建前缀prefix = self.indent_symbol * self.depthif index == len(items):if is_dir:print(f"{prefix}└── {self.folder_symbol}{item}")else:print(f"{prefix}└── {self.file_symbol}{item}")else:if is_dir:print(f"{prefix}├── {self.folder_symbol}{item}")else:print(f"{prefix}├── {self.file_symbol}{item}")# 递归处理子目录if is_dir:child_printer = DirectoryPrinter(root_dir=path,show_hidden=self.show_hidden,depth=self.depth + 1,max_depth=self.max_depth,indent_symbol=self.indent_symbol,folder_symbol=self.folder_symbol,file_symbol=self.file_symbol)child_printer.print_directory()def main():"""主函数,用户配置入口"""# 配置参数root_directory = input("请输入要打印的目录路径: ") or os.getcwd() # 默认为当前目录hide_hidden_files = input("是否隐藏隐藏文件? (y/n): ").lower() != 'y'max_depth = input("请输入最大递归深度 (留空表示无限制): ").strip() or None # 无限制auto_print = input("是否自动打印目录结构? (y/n): ").lower() == 'y'# 转换为整数try:max_depth = int(max_depth) if max_depth else Noneexcept ValueError:print("最大递归深度必须为整数")return# 打印配置print("\n=== 配置 ===")print(f"根目录: {root_directory}")print(f"隐藏文件和文件夹: {'是' if hide_hidden_files else '否'}")print(f"最大递归深度: {'无限制' if max_depth is None else max_depth}")# 初始化打印器printer = DirectoryPrinter(root_dir=root_directory,show_hidden=hide_hidden_files,max_depth=max_depth)# 开始打印if auto_print:print("\n=== 目录结构 ===")printer.print_directory()else:input("\n按下回车键开始打印目录结构...")print("\n=== 目录结构 ===")printer.print_directory()# 程序主入口
if __name__ == "__main__":main()