/Users/song/Code/script_python/code2md/main.py
| |
| |
| |
| import os |
| import re |
| import shutil |
| |
| import easygui |
| from easygui import * |
| |
| from pathlib import Path |
| |
| |
| class User(EgStore): |
| def __init__(self, filename): |
| self.path = '' |
| EgStore.__init__(self, filename) |
| |
| |
| def get_dir_path_gui(): |
| |
| user = User("settings.txt") |
| |
| user.restore() |
| file_path = easygui.diropenbox(default=user.path) |
| user.path = file_path |
| user.store() |
| return file_path |
| |
| |
| def get_root_dir(dir_path): |
| file_list = os.listdir(dir_path) |
| path_list = [] |
| root_file_list = [] |
| for file in file_list: |
| print(file) |
| |
| if file.startswith('.'): |
| continue |
| |
| is_file = re.findall(r'\.[^.\\/:*?"<>|\r\n]+$', file) |
| if len(is_file): |
| |
| res_abort = re.findall(re.compile( |
| r'(\.json|d\.ts|config\.ts|config\.js)$'), file) |
| if res_abort: |
| continue |
| |
| res_save = re.findall(re.compile(r'(\.py|vue|js|ts|html|txt)$'), file) |
| if len(res_save): |
| root_file_list.append(file) |
| continue |
| |
| res_abort = re.findall(re.compile( |
| r'(__pycache__|venv|build|dist|node_modules|public|LICENSE)'), file) |
| if len(res_abort): |
| continue |
| |
| file_path = os.path.join(dir_path, file) |
| path_list.append(file_path) |
| return path_list, root_file_list |
| |
| |
| def get_deep_dirs(path): |
| file_path = [] |
| for root, dirs, files in os.walk(path): |
| |
| |
| del_dir_index = [] |
| for i, dir in enumerate(dirs): |
| |
| if dir.startswith('.'): |
| del_dir_index.append(i) |
| |
| res_abort = re.findall(re.compile( |
| r'(__pycache__|venv|build|dist|node_modules|public|LICENSE|assets)'), dir) |
| if len(res_abort): |
| del_dir_index.append(i) |
| |
| |
| del_dir_index = list(set(del_dir_index)) |
| del_dir_index.sort() |
| for counter, index in enumerate(del_dir_index): |
| index = index - counter |
| dirs.pop(index) |
| |
| |
| del_file_index = [] |
| for i, file in enumerate(files): |
| |
| |
| if file.startswith('.'): |
| del_file_index.append(i) |
| |
| res_abort = re.findall(re.compile( |
| r'(\.json|\.d\.ts|\.lock|\.config\.ts|\.config\.js|\.png|\.woff2|\.ttf|\.woff|\.css|README\.md|\.toml|swagger-ui-bundle.js)$'), |
| file) |
| if len(res_abort): |
| del_file_index.append(i) |
| |
| |
| del_file_index = list(set(del_file_index)) |
| del_file_index.sort() |
| for counter, index in enumerate(del_file_index): |
| index = index - counter |
| files.pop(index) |
| |
| |
| for file in files: |
| |
| res_save = re.findall(re.compile(r'(\.py|vue|js|ts|html)$'), file) |
| if len(res_save): |
| file_path.append(os.path.join(root, file)) |
| return file_path |
| |
| |
| def readcode_writemd(file_path, root_path): |
| suffix = re.findall(r'\.[^.\\/:*?"<>|\r\n]+$', file_path) |
| if len(suffix): |
| suffix = suffix[0][1:] |
| with open(file_path, "r", encoding='utf-8') as f: |
| head_line = f.readline() |
| rest_line = f.read() |
| write2md(head_line, head_line + rest_line, |
| suffix, file_path, root_path) |
| |
| |
| def write2md(head, content, suffix, file_path, root_path): |
| with open(root_path + '/NOTE.md', "a", encoding='utf-8') as f: |
| f.write(f"# `{file_path}`\n\n") |
| |
| f.write(f"```{suffix}\n") |
| f.write(content) |
| f.write(f"```\n") |
| |
| |
| if __name__ == '__main__': |
| root_path = get_dir_path_gui() |
| md_file = os.path.join(root_path, '====NOTE.md') |
| |
| |
| if os.path.exists(md_file): |
| os.remove(md_file) |
| |
| file_path_list = get_deep_dirs(root_path) |
| for file_path in file_path_list: |
| print(file_path) |
| readcode_writemd(file_path, root_path) |
| print('!!!complete!!!') |
| os.system('open '+root_path) |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
2022-02-12 css 四种基本选择器
2022-02-12 四种基本选择器
2022-02-12 css的三种引入方式
2022-02-12 html中元素之间的关系
2022-02-12 块级元素 行内元素
2022-02-12 代码提示 thisArg argArray
2022-02-12 如何监听一个对象