[Python] 纯文本查看 复制代码
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.ttk import *
import sqlite3
import os
filetype_pre = "" # 上一个文件类型
items_pre = 1 # 上一个文件/文件夹所在目录层数
gaps="" # 总层数
def select_db_file():
db_file = askopenfilename(
title="请选择BaiduYunCacheFileV0.db文件", filetypes=[('db', '*.db')])
db.set(db_file)
def select_save_file():
save_file = asksaveasfilename(filetypes=[('文件', '*.txt')])
f.set(save_file+".txt")
def txtTOhtml(path):
'''
param txt文件路径\n
将生成好的html标签写入为html文件
'''
base=[
"<html><head><meta charset='UTF-8'><style> body { margin: 0; background-color: rgb(156, 215, 217); } p { color: aliceblue; } ul { list-style: none; } ul li ul { display: none; } #box1 { flex: 3; } .zk { display: block; } </style><script src='https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js'></script></head><body><div id='box1'><ul><!-- 目录树开始 -->\n",
"\n<!-- 目录树结束 --></ul></div><script> $('li').click(function (event) { obj = $(this).children('ul'); if (!obj.hasClass('zk')) { siblings = $(this).siblings().children('ul'); if (siblings.hasClass('zk')) { siblings.removeClass('zk'); }; $(obj).addClass('zk'); } else { $(obj).removeClass('zk'); }; event.stopPropagation(); }); </script></body></html>"
]
pathportion = os.path.splitext(path) # 分离文件名与扩展名
scratchFile=pathportion[0]+".html" # 拼接html文件名
#向html文件写入
f = open(scratchFile,'w',encoding='utf-8')
with open(path,'r',encoding='utf-8') as fa:
Readeddata = fa.readlines()
f.write(base[0]) #写入html固定头
for x in Readeddata:#写入生成的html标签
f.write(x)
f.write(base[1]) #写入html固定尾
f.close()
def html_tails(gaps):
'''
param gaps 最后的层\n
return html 文档末尾的标签
'''
html_tails=""
for i in range(gaps.count("┃")):
html_tails=html_tails+"</li></ul>"
return html_tails
def htmltxt(filetype_pre, filetypes, item_vary):
'''
获得对应的html标签\n
param filetype_pre 上个文件类型\n
param filetypes 当前文件类型\n
param item_vary 层数差
'''
html = {
"<li><p>": "<li><p>", # 文件夹头
"</p>": "</p>", # 文件夹尾
"<li>": "<li>", # 文件头
"</li>": "</li>", # 文件尾/
"<ul>": "<ul>",
"</ul></li>": "</ul></li>",
"</li></ul></li>": "</li></ul></li>"
}
html_txt = "" #当前文件与上个文件之间的结束标签
html_head = "" #文件/文件夹头标签
html_tail = "" #文件/文件夹尾标签
#True为文件夹 False为文件
if (filetype_pre == ""): # 上个文件类型=空
return html["<li><p>"], html["</p>"]
if (filetypes == True): # 当前文件类型=文件夹
html_head = html["<li><p>"]
html_tail = html["</p>"]
else:
html_head = html["<li>"]
html_tail = html["</li>"]
if (filetype_pre == True): # 上个文件类型=文件夹
if(item_vary==0):
html_txt=html["</li>"]
elif(item_vary==1):
html_txt=html["<ul>"]
elif(item_vary<0):
for i in range(abs(item_vary)):
html_txt=html_txt+html["</li></ul></li>"]
if(filetype_pre==False): #上个文件类型=文件
if(item_vary==0):
html_txt=""
else:
for i in range(abs(item_vary)): #按层数依次补充
html_txt=html_txt+html["</ul></li>"]
return html_txt+html_head, html_tail
def file_type(filename):
'''
判断文件是 文件夹True 还是 文件False
'''
types = [
".mp4", ".mp3", ".ts", ".pdf", ".docx", ".ppt", ".ppts", ".doc", ".7z", ".zip", ".xlsx", ".jpeg",".jpg", ".png", ".heic", ".mov", "3gp", "aac", "ace", "aif", "arj", "asf", "avi", "bin", "bz2", "exe", "gz", "gzip", "img", "iso", "lzh", "m4a", "m4v", "mkv", "mpa", "mpe", "mpeg", "mpg", "msi", "msu", "ogg", "ogv", "pdf", "plj", "pps", "qt", "ra", "rar", "rm", "rmvb", "sea", "sit", "sitx", "tar", "tif", "tiff", "wav", "wma", "wmv", "apk", "m3u8", "apks", "apkm"
]
if "." in filename:
for i in types:
if i in filename:
return False
for i in types:
if i.upper() in filename:
return False
return True
def filetype(filename, item):
'''
param filename 文件名\n
param item 当前目录\n
返回文件前后html标签\n
return 前标签,后标签
'''
#True为文件夹 False为文件
global filetype_pre # 上个文件类型
filetypes = True # 当前文件类型
# 判断当前文件类型
filetypes = file_type(filename)
global items_pre # 上个文件所在目录层数
items = item.count('/') # 当前文件所在目录层数
item_vary = items-items_pre # 当前目录与上个目录 层数变化
html_head = ""
html_tail = ""
html_head,html_tail=htmltxt(filetype_pre,filetypes,item_vary)
filetype_pre = filetypes # 更新上个文件类型
items_pre = items # 更新上个文件所在目录层数
return html_head, html_tail
def write_file(file_dict, f, item, gap=""):
'''
向txt写入
'''
global gaps
if item == "/":
for i in file_dict["/"]:
html_head,html_tail=filetype(i,"/")
f.write(html_head+ i + html_tail+"\n")
i = item + i + "/"
if i in file_dict:
write_file(file_dict, f, i, gap="┃ ")
else:
gap = "┃ " + gap
for i in file_dict[item]:
if(i==""):
print(item)
html_head,html_tail=filetype(i,item)
f.write(html_head+ i + html_tail+"\n")
gaps=""
gaps=gap
i = item + i + "/"
if i in file_dict:
write_file(file_dict, f, i, gap)
def create_baiduyun_filelist():
file_dict = {}
conn = sqlite3.connect(db.get())
cursor = conn.cursor()
cursor.execute("select * from cache_file")
while True:
value = cursor.fetchone()
if not value:
break
#路径
path = value[2]
#文件名
name = value[3]
#大小
size = value[4]
isdir = value[6]
if path not in file_dict:
file_dict[path] = []
file_dict[path].append(name)
else:
file_dict[path].append(name)
with open(f.get(), "w", encoding='utf-8') as fp:
write_file(file_dict, fp, "/")
global gaps
fp.write(html_tails(gaps))
txtTOhtml(fp.name)
root = Tk()
root.title('百度云文件列表生成工具')
db_select = Button(root, text=' 选择DB文件 ', command=select_db_file)
db_select.grid(row=1, column=1, sticky=W, padx=(2, 0), pady=(2, 0))
db = StringVar()
db_path = Entry(root, width=80, textvariable=db)
db_path['state'] = 'readonly'
db_path.grid(row=1, column=2, padx=3, pady=3, sticky=W+E)
save_path = Button(root, text='选择保存地址', command=select_save_file)
save_path.grid(row=2, column=1, sticky=W, padx=(2, 0), pady=(2, 0))
f = StringVar()
file_path = Entry(root, width=80, textvariable=f)
file_path['state'] = 'readonly'
file_path.grid(row=2, column=2, padx=3, pady=3, sticky=W+E)
create_btn = Button(root, text='生成文件列表', command=create_baiduyun_filelist)
create_btn.grid(row=3, column=1, columnspan=2, pady=(0, 2))
root.columnconfigure(2, weight=1)
root.mainloop()