大家写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
1161

被折叠的 条评论
为什么被折叠?



