首页 > 其他分享 >代码项目快速生成markdown文件

代码项目快速生成markdown文件

时间:2023-06-22 17:00:19浏览次数:34  
标签:index markdown 代码 list 生成 file path root dir

code2md/run_img2markdown.command

#! /bin/bash

source /Users/song/Code/script_python/code2md/venv/bin/activate

# echo -n '请任意拖入文件夹中的一个文件:'
# read file_path

python3 /Users/song/Code/script_python/code2md/main_img2markdown.py 





code2md/main_img2markdown.py

import os
import re
import sys
import time
import easygui
from easygui import *


from urllib.parse import quote

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

#将时间字符串转换为10位时间戳,时间字符串默认为2017-10-01 13:37:04格式
def date_to_timestamp(date, format_string="%Y-%m-%d %H:%M:%S"):
    time_array = time.strptime(date, format_string)
    time_stamp = int(time.mktime(time_array))
    return time_stamp


def sort_files(file_path):
    return sorted(file_path,key=lambda name:date_to_timestamp(name[2:-4],format_string="%Y-%m-%d %H.%M.%S"))
     


def get_images(path):
    file_path = []
    for root, dirs, files in os.walk(path):
        # 筛选所有符合后缀的文件------------------------------------------------------------------------
        for file in files:
            # 反向过滤
            res_abort = re.findall(re.compile(
                r'( \(2\)\.png)$'), file)
            if res_abort:
                continue
            res_save = re.findall(re.compile(r'(\.png|jpg)$'), file)
            if len(res_save):
                # file_path.append(quote('./'+file))
                file_path.append(file)

    return file_path


def write2md(index,img_path, root_path):
    with open(root_path + '/NOTE.md', "a", encoding='utf-8') as f:  # 打开文件
        f.write(f"# {index}\n\n")
        f.write(f"![img]({img_path})")
        f.write(f"\n\n\n")


if __name__ == '__main__':

    dir_name = get_dir_path_gui()


    md_file = os.path.join(dir_name, '====NOTE.md')
    # 清楚上一次的文件
    if os.path.exists(md_file):
        os.remove(md_file)

    img_path_list = get_images(dir_name)
    sorted_img_path_list = sort_files(img_path_list)
    parsed_sorted_img_path_list =   list(map(lambda file: quote('./'+file), sorted_img_path_list))   # 使用 lambda 匿名函数

    for index,img_path in enumerate(parsed_sorted_img_path_list):
        write2md(index,img_path, dir_name)
    print('!!!complete!!!')
    os.system('open '+dir_name)

code2md/code2md_多级标题.py

#!/usr/bin/env python3
import os
import subprocess
import re
import shutil
import sys
from pathlib import Path
from datetime import datetime



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'(d\.ts|config\.ts|-lock\.json)$'), file)
            if res_abort:
                continue
            # 保留根文件夹的(\.py|vue|js|ts)$ 结尾的文件
            res_save = re.findall(re.compile(r'(\.py|vue|config\.js|js|ts|html|txt|go|mod|json)$'), 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|vendor|tmp|static|templates)'), 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'(_NOTE\.md|\.d\.ts|\.lock|\.png|\.woff2|\.ttf|\.woff|\.css|README\.md|\.toml|swagger-ui-bundle.js|-lock\.json)$'),
                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|go|mod|json)$'), file)
            if len(res_save):
                file_path.append(os.path.join(root, file))
    return file_path


def readcode_writemd(file_path,root_path ,md_file_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:  # 打开文件
        rest_line = f.read()
        write2md(rest_line,suffix, file_path, root_path,md_file_path)



def get_md_title_path(file_path,root_path):
    # Get the common prefix of the two paths
    common_prefix = os.path.commonprefix([file_path, root_path])
    # Get the different parts of the two paths
    diff1 = file_path[len(common_prefix)+1:]
    # print(os.path.basename(root_path))
    # print(diff1)
    # print(os.path.join(os.path.basename(root_path),diff1))
    md_title = os.path.join(os.path.basename(root_path),diff1)
    return md_title



def write2md(content, suffix, file_path,root_path, md_file_path):
    with open(md_file_path, "a", encoding='utf-8') as f:  # 打开文件
        md_title = get_md_title_path(file_path,root_path)
        f.write(f"# `{md_title}`\n\n")
        f.write(f"```{suffix}\n")
        f.write(content)
        f.write(f"\n")
        f.write(f"```\n\n\n")



def get_root_path(path):
    dir_path = path
    # 判断当前文件是否是文件
    if os.path.isfile(path):
        dir_path = os.path.dirname(path)
    return dir_path



def get_file_name():
    # Get the current time
    now = datetime.now()
    # Format the time as a string
    time_str = now.strftime('%Y-%m-%d_%H-%M-%S')
    # Create the file name
    file_name = f'Z_{time_str}_NOTE.md'
    return file_name




if __name__ == '__main__':
    input_list = sys.argv[1].split(' ')
    input_file_path = input_list[0] 
    #  md 一级标题的名称
    md_lv_one = os.path.basename(input_file_path)

    if len(input_list) > 1:
        # Remove the first element
        input_list = input_list[1:]
        # Get the non-empty elements
        output_list = [x for x in input_list if x]
        md_lv_one = output_list[0] 



    root_path = get_root_path(input_file_path)
    md_file_name = get_file_name()
    md_file_path = os.path.join(root_path, md_file_name)
    file_path_list = get_deep_dirs(root_path)



    for i,file_path in enumerate(file_path_list):
        print(i,'->',get_md_title_path(file_path,root_path))
        readcode_writemd(file_path, root_path,md_file_path)

    print('=============done=============')

    # os.system('open '+root_path)

code2md/run_code2md.command

#! /bin/bash
# 激活虚拟环境,安装目前的情况,不需要虚拟环境,也可以实现
# source /home/song/venv/bin/activate
echo "请拖入一个代码项目根目录的文件或者文件夹:"
read file
python3 /Users/song/Code/script_python/code2md/code2md.py "$file"
# 等待五秒钟,然后自动关闭
sleep 3 
osascript -e 'tell application "Terminal" to close first window' & exit




code2md/test.py

import time

names = ["截屏2023-03-12 14.46.00.png",
"截屏2023-03-12 14.38.50.png",
"截屏2023-03-12 14.48.17.png",
"截屏2023-03-12 14.47.26.png",
"截屏2023-03-12 14.35.46.png",
"截屏2023-03-12 14.40.34.png",
"截屏2023-03-12 14.23.45.png",
"截屏2023-03-12 14.41.10.png"]


#将时间字符串转换为10位时间戳,时间字符串默认为2017-10-01 13:37:04格式
def date_to_timestamp(date, format_string="%Y-%m-%d %H:%M:%S"):
    time_array = time.strptime(date, format_string)
    time_stamp = int(time.mktime(time_array))
    return time_stamp


names.sort(key=lambda name:date_to_timestamp(name[2:-4],format_string="%Y-%m-%d %H.%M.%S"))

for name in names:
    print(name)


code2md/check_alias.sh

#!/bin/bash
read -p "Enter file path: " file_path
is_alias=$(GetFileInfo -aa "$file_path")
if [ "$is_alias" == "1" ]; then
    echo "yes" 
else
    echo "no" 
fi

code2md/code2md.py

#!/usr/bin/env python3
import os
import subprocess
import re
import shutil
import sys
from pathlib import Path
from datetime import datetime

# 需要过滤的文件夹
exclude_dirs = ['__pycache__', 'venv', 'build', 'dist', 'node_modules', 'public', 'LICENSE', 'assets', 'vendor', 'tmp', 'static', 'templates']
# 需要过滤文件后缀
exclude_files = ['_NOTE.md', '.d.ts', '.lock', '.png', '.woff2', '.ttf', '.woff', '.css', 'README.md', '.toml', 'swagger-ui-bundle.js', '-lock.json']
# 需要保留的文件
include_exts = ['.py', '.vue', '.js', '.ts', '.html', '.go', '.mod', '.json','.txt','.sh','.command']
# 
md_suffix_table = {
    'command': 'sh',
}


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'(d\.ts|config\.ts|-lock\.json)$'), file)
            if res_abort:
                continue
            # 保留根文件夹的(\.py|vue|js|ts)$ 结尾的文件
            res_save = re.findall(re.compile(r'(\.py|vue|config\.js|js|ts|html|txt|go|mod|json)$'), 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|vendor|tmp|static|templates)'), 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'(_NOTE\.md|\.d\.ts|\.lock|\.png|\.woff2|\.ttf|\.woff|\.css|README\.md|\.toml|swagger-ui-bundle.js|-lock\.json)$'),
                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|go|mod|json)$'), file)
            if len(res_save):
                file_path.append(os.path.join(root, file))
    return file_path

def get_deep_dirs_fast(path):
    global exclude_dirs
    global exclude_files
    global include_exts

    

    file_path = []
    for root, dirs, files in os.walk(path):
        # 过滤不符合的文件夹------------------------------------------------------------------------
        dirs[:] = [d for d in dirs if not d.startswith('.') and not any(ex in d for ex in exclude_dirs)]
        # 过滤不符合的文件-----------------------------------------------------------------------------
        files[:] = [f for f in files if not f.startswith('.') and not any(ex in f for ex in exclude_files)]
        # 筛选所有符合后缀的文件------------------------------------------------------------------------
        for file in files:
            # 正向过滤含有(\.py|vue|js|ts)$ 结尾的文件
            if any(file.endswith(ext) for ext in include_exts):
                file_path.append(os.path.join(root, file))
    return file_path



def readcode_writemd(file_path,root_path ,md_file_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:  # 打开文件
        try:
            rest_line = f.read()
        except Exception as e:
            print(f'{file_path}文件编码读取错误,非utf-8')
            rest_line = '' 
        write2md(rest_line,suffix, file_path, root_path,md_file_path)



def get_md_title_path(file_path,root_path):
    # Get the common prefix of the two paths
    common_prefix = os.path.commonprefix([file_path, root_path])
    # Get the different parts of the two paths
    diff1 = file_path[len(common_prefix)+1:]
    # print(os.path.basename(root_path))
    # print(diff1)
    # print(os.path.join(os.path.basename(root_path),diff1))
    md_title = os.path.join(os.path.basename(root_path),diff1)
    return md_title


def get_code_md_lable_by_suffix(suffix):
    global md_suffix_table

    if md_suffix_table.get(suffix) is not None:
        return md_suffix_table.get(suffix)

    return suffix


def write2md(content, suffix, file_path,root_path, md_file_path):
    with open(md_file_path, "a", encoding='utf-8') as f:  # 打开文件
        md_title = get_md_title_path(file_path,root_path)
        f.write(f"# `{md_title}`\n\n")
        f.write(f"```{get_code_md_lable_by_suffix(suffix)}\n")
        f.write(content)
        f.write(f"\n")
        f.write(f"```\n\n\n")



def get_root_path(path):
    dir_path = path
    # 判断当前文件是否是文件
    if os.path.isfile(path):
        dir_path = os.path.dirname(path)
    return dir_path



def get_file_name():
    # Get the current time
    now = datetime.now()
    # Format the time as a string
    time_str = now.strftime('%Y-%m-%d_%H-%M-%S')
    # Create the file name
    file_name = f'Z_{time_str}_NOTE.md'
    return file_name




if __name__ == '__main__':
    root_path = get_root_path(sys.argv[1])
    md_file_name = get_file_name()
    md_file_path = os.path.join(root_path, md_file_name)

    file_path_list = get_deep_dirs_fast(root_path)


    for i,file_path in enumerate(file_path_list):
        print(i,'->',get_md_title_path(file_path,root_path))
        readcode_writemd(file_path, root_path,md_file_path)

    print('=============done=============')

    # os.system('open '+root_path)

code2md/judge.command

#!/bin/bash
read -p "Enter file path: " file_path

is_alias=$(GetFileInfo -aa "$file_path")

if [ "$is_alias" == "1" ]; then
    echo "yes" 
else
    echo "no" 
fi

code2md/run.command

#! /bin/bash

source /Users/song/Code/script_python/code2md/venv/bin/activate

python /Users/song/Code/script_python/code2md/main.py


code2md/main.py

#!/usr/bin/env python3

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'(d\.ts|config\.ts|-lock\.json)$'), file)
            if res_abort:
                continue
            # 保留根文件夹的(\.py|vue|js|ts)$ 结尾的文件
            res_save = re.findall(re.compile(r'(\.py|vue|config\.js|js|ts|html|txt|go|mod|json)$'), 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|vendor|tmp|static|templates)'), 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'(\.d\.ts|\.lock|\.png|\.woff2|\.ttf|\.woff|\.css|README\.md|\.toml|swagger-ui-bundle.js|-lock\.json)$'),
                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|go|mod|json)$'), 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")
        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)

code2md/get_all_files.py

#!/usr/bin/env python3
import os
import subprocess
import re
import shutil
import sys
from pathlib import Path
from datetime import datetime



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'(d\.ts|config\.ts|-lock\.json)$'), file)
            if res_abort:
                continue
            # 保留根文件夹的(\.py|vue|js|ts)$ 结尾的文件
            res_save = re.findall(re.compile(r'(\.py|vue|config\.js|js|ts|html|txt|go|mod|json)$'), 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):
        print(dirs)
        print(files)
        # print(os.path.join(root, file))
        print('--------------------')
        # 过滤不符合的文件夹------------------------------------------------------------------------
        wait_del_dir_idxs = [] # 带删除的文件夹的下标
        for i, dir in enumerate(dirs):
            # 过滤隐藏文件
            if dir.startswith('.'):
                wait_del_dir_idxs.append(i)
            # 过滤掉所有不符合的文件夹
            res_abort = re.findall(re.compile(
                r'(__pycache__|venv|build|dist|node_modules|public|LICENSE|assets|vendor|tmp|static|templates)'), dir)
            if len(res_abort):
                wait_del_dir_idxs.append(i)
    

        # 去重,排序,过滤文件夹
        wait_del_dir_idxs = list(set(wait_del_dir_idxs))
        wait_del_dir_idxs.sort()
        for counter, index in enumerate(wait_del_dir_idxs):
            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'(_NOTE\.md|\.d\.ts|\.lock|\.png|\.woff2|\.ttf|\.woff|\.css|README\.md|\.toml|swagger-ui-bundle.js|-lock\.json)$'),
                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|go|mod|json)$'), file)
            if len(res_save):
                file_path.append(os.path.join(root, file))
    return file_path





def get_deep_dirs_fast(path, exclude_dirs=None, exclude_files=None, include_exts=None):
    # 需要过滤的文件夹
    if exclude_dirs is None:
        exclude_dirs = ['__pycache__', 'venv', 'build', 'dist', 'node_modules', 'public', 'LICENSE', 'assets', 'vendor', 'tmp', 'static', 'templates']
    # 需要过滤文件后缀
    if exclude_files is None:
        exclude_files = ['_NOTE.md', '.d.ts', '.lock', '.png', '.woff2', '.ttf', '.woff', '.css', 'README.md', '.toml', 'swagger-ui-bundle.js', '-lock.json']
    # 需要保留的文件
    if include_exts is None:
        include_exts = ['.py', '.vue', '.js', '.ts', '.html', '.go', '.mod', '.json','.txt']

    file_path = []
    for root, dirs, files in os.walk(path):
        # 过滤不符合的文件夹------------------------------------------------------------------------
        dirs[:] = [d for d in dirs if not d.startswith('.') and not any(ex in d for ex in exclude_dirs)]
        # 过滤不符合的文件-----------------------------------------------------------------------------
        files[:] = [f for f in files if not f.startswith('.') and not any(ex in f for ex in exclude_files)]
        # 筛选所有符合后缀的文件------------------------------------------------------------------------
        for file in files:
            # 正向过滤含有(\.py|vue|js|ts)$ 结尾的文件
            if any(file.endswith(ext) for ext in include_exts):
                file_path.append(os.path.join(root, file))
    return file_path










if __name__ == '__main__':
    root_path = "/Users/song/Code/agv_frontend"
    file_path_list = get_deep_dirs_fast(root_path)
    for file_path in file_path_list:
        print(file_path)
    
    print(f'=============done_{len(file_path_list)}=============')

标签:index,markdown,代码,list,生成,file,path,root,dir
From: https://www.cnblogs.com/zhuoss/p/17498003.html

相关文章

  • 从优秀代码到良好代码的8个步骤
    目录《从优秀代码到良好代码的8个步骤》:代码管理+版本控制+持续集成+自动化测试+代码规范+代码质量评估+持续改进+团队协作引言编写高质量的代码是软件开发中至关重要的一步。然而,在编写高质量的代码之前,需要对代码进行有效的管理、版本控制和持续集成,以确保代码......
  • 代码质量与代码优化:如何优化代码性能和代码可读性
    目录代码质量与代码优化:如何优化代码性能和代码可读性背景介绍在现代软件开发中,代码质量是非常重要的一个方面。一个好的代码不仅可以更快地完成开发任务,还可以更好地维护和扩展。为了提高代码质量,我们需要对代码进行优化,以便更好地实现我们的目标和需求。代码优化包括优化代......
  • Android Xml文件生成,Xml数据格式写入
    生成xml文件格式数据,Android提供了Xml.newSerializer();,可以理解为Xml序列化;序列化:把内存里面的数据(file,databases,xml等等)丢给某一个地方; 反序列化:把某个地方的数据(file,databases,xml等等),拿到内存中;既然是Android操作Xml,就用Android所提供的API,不用Java所提供的API,DOM......
  • 【代码设计】链表结构解决多流程校验
    目的使用合理的代码设计,解决业务场景的中的实际问题。背景介绍在实际的业务场景中,用户的一个操作行为,是否允许真正被执行,往往会涉及到多流程的校验,一旦有条件不满足将会被中止。以下面流程图为例:用户点击了打赏按钮,会进行是否有网络检查,没有网络,会有网络连接弹框,等待用户连接结果......
  • 添加一段代码,让你的网站在微信QQ提示使用浏览器访问
    <script>//跳转提示if(is_weixn_qq()){;window.location.href='https://c.pc.qq.com/middle.html?pfurl='+window.location.href;}functionis_weixn_qq(){//判断当前是否微信/QQ浏览器varua=navigator.userAgent;varisWeixin=!!/MicroMessenger/i.test......
  • python通过字典生成随机城市
    先把省和城市存入一个字典中用到random模块中的choice方法,在列表中随机选一个元素。 importrandom省=random.choice(list(city.keys()))#把字典的键表转换成列表,再随机选一个市=random.choice(city[省])#指定省后,在城市列表中随机选一个print(省,市) ......
  • sentence-transformers(SBert)中文文本相似度预测(附代码)
    sentence-transformers(SBert)中文文本相似度预测(附代码)https://blog.csdn.net/weixin_54218079/article/details/128687878https://gitee.com/liheng103/sbert-evaluatehttps://www.sbert.net/ 训练模型创建网络:使用Sbert官方给出的预训练模型sentence_hfl_chinese-rober......
  • 数据分享|PYTHON用决策树分类预测糖尿病和可视化实例|附代码数据
    全文下载链接:http://tecdat.cn/?p=23848最近我们被客户要求撰写关于决策树的研究报告,包括一些图形和统计输出。在本文中,决策树是对例子进行分类的一种简单表示。它是一种有监督的机器学习技术,数据根据某个参数被连续分割。决策树分析可以帮助解决分类和回归问题决策树算法将数......
  • R语言ggmap空间可视化机动车交通事故地图|附代码数据
    原文链接:http://tecdat.cn/?p=12350最近我们被客户要求撰写关于空间可视化的研究报告,包括一些图形和统计输出。在本文中,我使用ggmap可视化纽约市的交通事故数据来自纽约市开放数据。我的数据范围是2012年至2015年。该数据跟踪车辆的类型,发生事故的街道的名称以及事故的经度和纬......
  • R语言BUGS/JAGS贝叶斯分析: 马尔科夫链蒙特卡洛方法(MCMC)采样|附代码数据
    全文链接:http://tecdat.cn/?p=17884最近我们被客户要求撰写关于BUGS/JAGS贝叶斯分析的研究报告,包括一些图形和统计输出。在许多情况下,我们没有足够的计算能力评估空间中所有n维像素的后验概率 。在这些情况下,我们倾向于利用称为Markov-ChainMonteCarlo算法的程序 。此方法......