当前位置: 首页 > news >正文

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_symbolfolder_symbolfile_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()
http://www.lryc.cn/news/581723.html

相关文章:

  • 暑假算法日记第二天
  • 李宏毅genai笔记:LLM内部机制
  • ubuntu 安装 MQTT服务器 mosquitto homeassistant 并在HA中集成MQTT
  • Solidity——returns和return区别、命名式返回、解构式赋值
  • 【代码问题】【模型下载】从 HuggingFace 上下载模型
  • 2、Connecting to Kafka
  • 大模型关键字解释
  • 【机器学习笔记Ⅰ】4 梯度下降
  • 【管理学】乐嘉性格色彩与MBTI的优劣和适用场景
  • 【C++基础】内存管理四重奏:malloc/free vs new/delete - 面试高频考点与真题解析
  • 汇编与接口技术:8259中断实验
  • 高效处理大体积Excel文件的Java技术方案解析
  • 从0写自己的操作系统(4)实现简单的任务切换
  • FileZilla二次开发实战指南:C++架构解析与界面功能扩展
  • 在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控
  • 【机器学习笔记Ⅰ】13 正则化代价函数
  • [2025CVPR]一种新颖的视觉与记忆双适配器(Visual and Memory Dual Adapter, VMDA)
  • SSL 终结(SSL Termination)深度解析:从原理到实践的全维度指南
  • Python Bcrypt详解:从原理到实战的安全密码存储方案
  • 用户中心Vue3项目开发2.0
  • 2048小游戏实现
  • 线性代数--AI数学基础复习
  • 深度学习6(多分类+交叉熵损失原理+手写数字识别案例TensorFlow)
  • Chunking-free RAG
  • Web-API-day2 间歇函数setInterval与事件监听addEvenListener
  • 【Note】《Kafka: The Definitive Guide》第四章:Kafka 消费者全面解析:如何从 Kafka 高效读取消息
  • Apache Spark 4.0:将大数据分析提升到新的水平
  • A O P
  • 金融级B端页面风控设计:操作留痕与异常预警的可视化方案
  • 深度学习篇---深度学习常见的应用场景