import os
import re
import shutil

import easygui
from easygui import *


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 = []
    for file in file_list:
        # 过滤隐藏文件
        if file.startswith('.'):
            continue
        # 过滤所有的文件
        is_file = re.findall(r'\.[^.\\/:*?"<>|\r\n]+$', file)
        if len(is_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


def setDir(filepath):
    '''
    如果文件夹不存在就创建,如果文件存在就清空!
    :param filepath:需要创建的文件夹路径
    :return:
    '''
    if not os.path.exists(filepath):
        os.mkdir(filepath)
    else:
        shutil.rmtree(filepath, ignore_errors=True)
        os.mkdir(filepath)


def cnblog2anki(file):
    path_list = []
    with open(file, "r", encoding='utf-16') as f:  # 打开文件
        bookmarks = f.readlines()  # 读取文件
        for index, bookmark in enumerate(bookmarks):
            # 第一行和最后一行过滤掉
            if index == 0 or bookmark == '\n':
                continue
            path = bookmark.split('=', 1)[1].strip()
            path_list = path.split('\\')
            new_path = ".\\AA提取\\" + path_list.pop()
            if index == 1:
                mkdir(".\\AA提取")
            shutil.copy(path, new_path)


def mkdir(path):
    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    path = path.rstrip("\\")

    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)

    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print('已存在')




def get_deep_dirs(path):
    file_path = []
    for root, dirs, files in os.walk(path):
        # 过滤隐藏文件
        for file in files:
            # 反向过滤,后缀文件
            res_abort = re.findall(re.compile(r'(\.json|d\.ts)$'), file)
            if res_abort:
                continue
            # 反向过滤包含特定字符的文件
            res_abort2 = re.findall(re.compile(r'(assets)'), file)
            if len(res_abort2):
                continue
            # 正向过滤含有(\.py|vue|js|ts)$ 结尾的文件
            res_save = re.findall(re.compile(r'(\.py|vue|js|ts)$'), file)
            if len(res_save):
                file_path.append(os.path.join(root, file))
    return file_path


def read_file(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"# {head}\n\n")
        f.write(f"## {file_path}\n")
        f.write(f"```{suffix}\n")
        f.write(content)
        f.write(f"```\n")

if __name__ == '__main__':
    root_path = get_dir_path_gui()
    res = get_root_dir(root_path)
    for dir_path in res:
        file_path = get_deep_dirs(dir_path)
        for file in file_path:
            read_file(file,root_path)
```

原文地址:http://www.cnblogs.com/zhuoss/p/16891174.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性