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)
```
标签:os,res,code2md,re,file,path,root
From: https://www.cnblogs.com/zhuoss/p/16891174.html