百度网盘读取全部文件列表

想导出百度网盘里的完整文件目录树,又不想一个个手动翻。百度网盘 PC 版会在本地缓存一份 SQLite 数据库,直接读出来转成 txt 就行。

代码来源于网上,很遗憾找不到原始出处了。

原理

安装百度网盘 PC 版本后,BaiduYunCacheFileV0.db 一般放在安装目录下的 user 目录里。这个文件本质上是单文件 SQLite 数据库,用 Python sqlite3 库读取 cache_file 表,再递归输出成树形 txt。

完整脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sqlite3
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.ttk import *


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 write_file(file_dict, f, item, gap=""):
if item == "/":
f.write("━" + "/" + "\n")
for i in file_dict["/"]:
f.write("┣" + "━" + i + "\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]:
f.write(gap + i + "\n")
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]
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, "/")


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()

运行后选择 .db 文件和保存路径,点击「生成文件列表」即可。

Notice: 正常情况下,这里会有一个基于utteranc.es的留言系统,如果看不到,可能要想想办法才能看到。

Powered by Hexo and Hexo-theme-hiker

Copyright © 2012 - 2026 tiaobug.com All Rights Reserved.

鲁ICP备2024124237号-1