D:\code_gitee\python_code2md_gitee\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
# 保留根文件夹的(\.py|vue|js|ts)$ 结尾的文件
res_save = re.findall(re.compile(r'(\.py|vue|js|ts|html)$'), file)
if len(res_save):
root_file_list.append(file)
continue
# 过滤node_modules
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):
# 过滤隐藏文件
# (\.gitignore)|(\.prettierrc)
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)$'),
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:
# 正向过滤含有(\.py|vue|js|ts)$ 结尾的文件
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"# {head}\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!!!')
原文地址:http://www.cnblogs.com/zhuoss/p/16897849.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性