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

python树状打印项目路径

学习这个的需求来自于,我想把项目架构告诉gpt问问它,然后不太会打印项目架构😂 联想到Linux的tree指令

import osclass DirectoryTree:def __init__(self, path):self.path = pathdef print_tree(self, method='default'):if method == 'default':self._print_dir_tree(self.path)elif method == 'with_count':self._print_dir_tree_with_count(self.path)elif method == 'files_only':self._print_files_only(self.path)elif method == 'with_symbols':self._print_dir_tree_with_symbols(self.path)else:print("Invalid method!")def _print_dir_tree(self, path, indent=0):print(' ' * indent + '|---' + os.path.basename(path))for item in os.listdir(path):itempath = os.path.join(path, item)if os.path.isdir(itempath):self._print_dir_tree(itempath, indent + 2)else:print(' ' * (indent + 2) + '|---' + item)def _print_dir_tree_with_count(self, path, indent=0):print(' ' * indent + '|---' + os.path.basename(path), end=' ')items = os.listdir(path)dirs = sum(1 for item in items if os.path.isdir(os.path.join(path, item)))files = len(items) - dirsprint(f"(dirs: {dirs}, files: {files})")for item in items:itempath = os.path.join(path, item)if os.path.isdir(itempath):self._print_dir_tree_with_count(itempath, indent + 2)else:print(' ' * (indent + 2) + '|---' + item)def _print_files_only(self, path, indent=0):for item in os.listdir(path):itempath = os.path.join(path, item)if os.path.isdir(itempath):self._print_files_only(itempath, indent)else:print(' ' * indent + '|---' + item)def _print_dir_tree_with_symbols(self, path, indent=0):print(' ' * indent + '📁 ' + os.path.basename(path))for item in os.listdir(path):itempath = os.path.join(path, item)if os.path.isdir(itempath):self._print_dir_tree_with_symbols(itempath, indent + 2)else:print(' ' * indent + '📄 ' + item)class EnhancedDirectoryTree:def __init__(self, path):self.path = pathself.show_hidden = Falseself.depth_limit = Noneself.show_files = Trueself.show_dirs = Truedef display_settings(self, show_hidden=False, depth_limit=None, show_files=True, show_dirs=True):self.show_hidden = show_hiddenself.depth_limit = depth_limitself.show_files = show_filesself.show_dirs = show_dirsdef print_tree(self):self._print_dir(self.path)def _print_dir(self, path, indent=0, depth=0):if self.depth_limit is not None and depth > self.depth_limit:returnif self.show_dirs:print(' ' * indent + '📁 ' + os.path.basename(path))for item in sorted(os.listdir(path)):# Check for hidden files/foldersif not self.show_hidden and item.startswith('.'):continueitempath = os.path.join(path, item)if os.path.isdir(itempath):self._print_dir(itempath, indent + 2, depth + 1)else:if self.show_files:print(' ' * indent + '📄 ' + item)# 创建类的实例
tree = DirectoryTree(os.getcwd())# 使用默认方法打印
print("Default:")
tree.print_tree()# 使用带计数的方法打印
print("\nWith Count:")
tree.print_tree(method='with_count')# 使用只打印文件的方法
print("\nFiles Only:")
tree.print_tree(method='files_only')# 使用emoji
print("\nWith Symbols:")
tree.print_tree(method='with_symbols')print('================================================================')enhancedtree = EnhancedDirectoryTree(os.getcwd())# Default print
enhancedtree.print_tree()print("\n---\n")# Configure settings to show hidden files, but only up to depth 2 and only directories
enhancedtree.display_settings(show_hidden=True, depth_limit=2, show_files=False)
enhancedtree.print_tree()print("\n---\n")# Configure settings to show only files up to depth 1
enhancedtree.display_settings(show_files=True, show_dirs=False, depth_limit=1)
enhancedtree.print_tree()
  • os.getcwd(): 返回当前工作目录的路径名。

  • os.path.basename(path): 返回路径名 path 的基本名称,即去掉路径中的目录部分,只保留文件或目录的名称部分。

  • os.listdir(path): 返回指定路径 path 下的所有文件和目录的名称列表。

  • os.path.isdir(path): 判断路径 path 是否为一个目录,如果是则返回 True,否则返回 False

  • os.path.join(path, *paths): 将多个路径组合成一个完整的路径名。这个函数会自动根据操作系统的不同使用不同的路径分隔符。

  • 上面两个类里面的函数其实就是用到递归,第二个类的那个函数可以设置递归深度

相关文章:

python树状打印项目路径

学习这个的需求来自于,我想把项目架构告诉gpt问问它,然后不太会打印项目架构😂 联想到Linux的tree指令 import osclass DirectoryTree:def __init__(self, path):self.path pathdef print_tree(self, methoddefault):if method default:sel…...

mysql误删误操作恢复数据,比传统方式和binlog2sql更快速用的恢复方式-reverse_sql恢复数据(单表多表)

场景: 误操作删除了某个表的数据,本文只讲工具的使用,首先自己通过mysqlbinlog或者记录找到误操作的时间范围:开始时间和结束时间,已经确定好是哪个binlog了下面以误删为例。 查看binlog是否开启 show variables like …...

CORE: Cooperative Reconstruction for Multi-Agent Perception 论文阅读

论文连接 CORE: Cooperative Reconstruction for Multi-Agent Perception 0. 摘要 本文提出了 CORE,一种概念简单、有效且通信高效的多智能体协作感知模型。 从合作重建的新颖角度解决了该任务: 合作主体共同提供对环境的更全面的观察整体观察可以作为…...

MySQL连接方式: Unix套接字 TCP/IP

今天连接mysql数据库使用mysql -u root -p指令的时候遇到了这个问题: 解决之后来总结一下mysql的连接方式 文章目录 1. Unix套接字(或Windows命名管道)特点:场景: 2. TCP/IP特点:场景: 3.对比总…...

TSINGSEE青犀智慧城市数字基座解决方案,助力城市数字化转型

一、行业背景 我国“十四五”规划纲要中提出,分级分类推进新型智慧城市建设,将物联网感知设施、通信系统等纳入公共基础设施统一规划建设,推进市政公用设施、建筑等物联网应用和智能化改造。完善城市信息模型平台和运行管理服务平台&#xf…...

【JavaEE】初识网络

网络初识 文章目录 网络初识网络发展史独立模式网络互连局域网LAN广域网 网络通信基础IP地址端口号格式 协议五元组协议分层OSI七层模型TCP/IP五(四)层协议1.物理层2.数据链路层3.网络层4.传输层5.应用程序 网络设备所在的分层封装和分用例子发送方接收方…...

UGUI交互组件ScrollBar

一.ScrollBar的结构 对象说明Scrollbar挂有Image和Scrollbar组件的主体对象Sliding Area表示滑动范围Handle滑块 二.Scrollbar的属性 属性说明Handle Rect控制柄对象的引用Direction拖动控制柄时滚动条值增加的方向Value滚动条的当前值,范围为 0.0 到 1.0Suze控制柄…...

DamiBus v0.51 发布

DamiBus,专为本地多模块之间通讯解耦而设计(尤其是未知模块、隔离模块、领域模块)。零依赖,特适合 DDD。 特点 结合 Bus 与 RPC 的概念,可作事件分发,可作接口调用,可作响应订阅。 支持事务传…...

[OpenJDK:环境变量配置]:填充Profile并修改默认配置

文章目录 一:背景:安装hadoop启动提示未找到JAVA_HOME1.1:配置Hadoop的Java环境变量 二:排查-定位解决2.1:查看环境变量配置发现没有JAVA_HOME2.1.1:解决:查看java安装目录2.1.2:再次…...

连接mysql报错 :Host ‘xxx.xx.x.x‘ is not allowed to connect to this MySQL server

排查思路 命令行连接mysql 我是安装在本地docker容器中的,可以使用一下命令直接连接 docker exec -it mysqldb mysql -u root -p其中mysqldb是我的mysql容器名字,后面跟随mysql的连接命令,然后输入密码即可。 如果没有设置过密码&#xff0c…...

Qt 布局(QSplitter 类QDockWidget 类) 总结

一、QSplitter 类(窗口分割) QSplitter类是一个Qt框架提供的基础窗口控件类,用于分割窗口,使得用户可以通过拖动分隔条来调节子窗口的大小。QSplitter在用户界面设计中非常常见,经常用于划分窗口区域,使得程序可以同时显示多个子…...

git-ssh-key协议同步文件

生成秘钥 ssh-keygen -t rsa ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/Beza/.ssh/id_rsa): /c/Users/Beza/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): …...

2018-2019 ACM-ICPC, Asia Nanjing Regional Contest G. Pyramid(组合数学 计数)

题目 t(t<1e6)组样例&#xff0c;每次给定一个n(n<1e9)&#xff0c;统计边长为n的上述三角形的等边三角形个数 其中等边三角形的三个顶点&#xff0c;可以在所有黑色三角形&白色三角形的顶点中任取&#xff0c; 答案对1e97取模 思路来源 申老师 & oeis A0003…...

C++学习——string 详解(即C++字符串详解)

以下内容源于C语言中文网的学习与整理&#xff0c;非原创&#xff0c;如有侵权请告知删除。 一、定义string变量的方法 C增强了对字符串的支持&#xff0c;除了可以使用C风格的字符串&#xff0c;还可以使用内置的 string 类。 string是类&#xff0c;而不是基本数据类型。虽…...

LeetCode 1 两数之和

题目描述 链接&#xff1a;https://leetcode.cn/problems/two-sum/?envTypefeatured-list&envId2ckc81c?envTypefeatured-list&envId2ckc81c 难度&#xff1a;简单 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 targ…...

【opencv】windows10下opencv4.8.0-cuda Python版本源码编译教程

【opencv】windows10下opencv4.8.0-cuda Python版本源码编译教程 提示:博主取舍了很多大佬的博文并亲测有效,分享笔记邀大家共同学习讨论 文章目录 【opencv】windows10下opencv4.8.0-cuda Python版本源码编译教程前言准备工具anaconda/cuda/cudnnanaconda创建环境(选做)安装原…...

【1day】用友U8Cloud未授权访问漏洞学习

注:该文章来自作者日常学习笔记,请勿利用文章内的相关技术从事非法测试,如因此产生的一切不良后果与作者无关。 目录 一、漏洞描述 二、影响版本 三、资产测绘 四、漏洞复现...

基于单片机智能汽车仪表设计系统

基于单片机的汽车智能仪表的设计 摘要&#xff1a;汽车的汽车系统。速度测量以及调速是我们这次的设计所要研究的对象&#xff0c;本次设计的基础核心的模块就是单片机&#xff0c;其应用的核心的控制单元就是stc89c52单片机&#xff0c;用到的测速模块是霍尔传感器&#xff0c…...

java double 保留两位小数

在Java中&#xff0c;你可以使用 DecimalFormat 或 String.format 来保留 double 类型的数字两位小数。以下是两个例子&#xff1a; 使用 DecimalFormat import java.text.DecimalFormat;public class Main {public static void main(String[] args) {double number 123.456…...

计网第六章(应用层)(三)(文件传输协议FTP)

一、基本概念 将某台计算机中的文件通过网络传送到可能相距很远的另一台计算机中即文件传送。 FTP就是因特网上使用得最广泛的文件传送协议。采用客户/服务器方式。 FTP提供交互式的访问&#xff0c;允许客户指明文件的类型和格式&#xff08;如指明是否使用ASCII码&#xf…...

C++_核心编程_多态案例二-制作饮品

#include <iostream> #include <string> using namespace std;/*制作饮品的大致流程为&#xff1a;煮水 - 冲泡 - 倒入杯中 - 加入辅料 利用多态技术实现本案例&#xff0c;提供抽象制作饮品基类&#xff0c;提供子类制作咖啡和茶叶*//*基类*/ class AbstractDr…...

盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来

一、破局&#xff1a;PCB行业的时代之问 在数字经济蓬勃发展的浪潮中&#xff0c;PCB&#xff08;印制电路板&#xff09;作为 “电子产品之母”&#xff0c;其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透&#xff0c;PCB行业面临着前所未有的挑战与机遇。产品迭代…...

Admin.Net中的消息通信SignalR解释

定义集线器接口 IOnlineUserHub public interface IOnlineUserHub {/// 在线用户列表Task OnlineUserList(OnlineUserList context);/// 强制下线Task ForceOffline(object context);/// 发布站内消息Task PublicNotice(SysNotice context);/// 接收消息Task ReceiveMessage(…...

渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止

<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet&#xff1a; https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...

Linux简单的操作

ls ls 查看当前目录 ll 查看详细内容 ls -a 查看所有的内容 ls --help 查看方法文档 pwd pwd 查看当前路径 cd cd 转路径 cd .. 转上一级路径 cd 名 转换路径 …...

Robots.txt 文件

什么是robots.txt&#xff1f; robots.txt 是一个位于网站根目录下的文本文件&#xff08;如&#xff1a;https://example.com/robots.txt&#xff09;&#xff0c;它用于指导网络爬虫&#xff08;如搜索引擎的蜘蛛程序&#xff09;如何抓取该网站的内容。这个文件遵循 Robots…...

NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合

在汽车智能化的汹涌浪潮中&#xff0c;车辆不再仅仅是传统的交通工具&#xff0c;而是逐步演变为高度智能的移动终端。这一转变的核心支撑&#xff0c;来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒&#xff08;T-Box&#xff09;方案&#xff1a;NXP S32K146 与…...

关于uniapp展示PDF的解决方案

在 UniApp 的 H5 环境中使用 pdf-vue3 组件可以实现完整的 PDF 预览功能。以下是详细实现步骤和注意事项&#xff1a; 一、安装依赖 安装 pdf-vue3 和 PDF.js 核心库&#xff1a; npm install pdf-vue3 pdfjs-dist二、基本使用示例 <template><view class"con…...

算法—栈系列

一&#xff1a;删除字符串中的所有相邻重复项 class Solution { public:string removeDuplicates(string s) {stack<char> st;for(int i 0; i < s.size(); i){char target s[i];if(!st.empty() && target st.top())st.pop();elsest.push(s[i]);}string ret…...

Docker、Wsl 打包迁移环境

电脑需要开启wsl2 可以使用wsl -v 查看当前的版本 wsl -v WSL 版本&#xff1a; 2.2.4.0 内核版本&#xff1a; 5.15.153.1-2 WSLg 版本&#xff1a; 1.0.61 MSRDC 版本&#xff1a; 1.2.5326 Direct3D 版本&#xff1a; 1.611.1-81528511 DXCore 版本&#xff1a; 10.0.2609…...