python工程文件结构梳理

大家写Python项目的时候是不是经常需要去向AI介绍自己的项目??然后让他帮忙写出部分代码        但是AI可能并不知道我们文件结构所以就不知如何引用以及我们要梳理的逻辑关系,所以我特意让AI写了一个可以获取本项目工程的文件结构的代码:

structure.py

方法:新建structure.py文件放在项目根目录,然后运行即可

  功能:   打印文件结构

1.支持不选中制定文件格式     如大量的.jpg素材                         修改 exclude_extensions

2.支持不选中某些文件夹        如.idea文件夹,images文件夹    修改exclude_dirs

源码:

import os

def list_directory(startpath, depth=0, exclude_dirs=None, exclude_extensions=None):
    if exclude_dirs is None:
        exclude_dirs = []
    if exclude_extensions is None:
        exclude_extensions = []

    for root, dirs, files in os.walk(startpath):
        # 当前目录的深度
        level = root.replace(startpath, "").count(os.sep)

        # 检查是否达到最大深度
        if depth and level > depth:
            continue

        # 检查当前目录是否在排除列表中
        if any(excluded in os.path.relpath(root, startpath).split(os.sep) for excluded in exclude_dirs):
            dirs[:] = []  # 防止深入排除的目录
            continue

        indent = "│   " * level + "├── "
        print(f"{indent}{os.path.basename(root)}/")

        subindent = "│   " * (level + 1) + "├── "
        for f in files:
            if any(f.lower().endswith(ext.lower()) for ext in exclude_extensions):
                continue  # 跳过不需要的文件后缀
            print(f"{subindent}{f}")

# 示例使用
scan_path = os.getcwd()#curent work dictionary

exclude_dirs = [".idea"]#排除的目录
exclude_extensions = [".jpg",".png",".jpeg" ,".pyc"]  # 想跳过的文件类型

list_directory(scan_path, exclude_dirs=exclude_dirs, exclude_extensions=exclude_extensions)

结果示例:

D:\escalator\pyqt5\env_pyqt5\Scripts\python.exe D:\escalator\pyqt5\escalator_pyqt5\structure.py 
├── escalator_pyqt5/
│   ├── config.py
│   ├── main.pro
│   ├── main.py
│   ├── structure.py
│   ├── test.py
│   ├── app/
│   │   ├── page_init.py
│   │   ├── common/
│   │   │   ├── config.py
│   │   │   ├── icon.py
│   │   │   ├── resource.py
│   │   │   ├── setting.py
│   │   │   ├── signal_bus.py
│   │   │   ├── style_sheet.py
│   │   │   ├── __pycache__/
│   │   ├── my_function/
│   │   │   ├── get_system_staus.py
│   │   │   ├── save_parameters.py
│   │   │   ├── some_function.py
│   │   │   ├── system_thread.py
│   │   │   ├── __pycache__/
│   │   ├── my_page/
│   │   │   ├── Computer_Status.py
│   │   │   ├── Computer_Status.ui
│   │   │   ├── Detection.py
│   │   │   ├── Detection.ui
│   │   │   ├── historical_record.py
│   │   │   ├── HomePage.py
│   │   │   ├── HomePage.ui
│   │   │   ├── Parameter_Setting.py
│   │   │   ├── Parameter_Setting.ui
│   │   │   ├── Real_time_camera.py
│   │   │   ├── Real_time_camera.ui
│   │   │   ├── Running_Summary.py
│   │   │   ├── Video.py
│   │   │   ├── Video.ui

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值