-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathupdate_readme.py
More file actions
51 lines (36 loc) · 1.58 KB
/
update_readme.py
File metadata and controls
51 lines (36 loc) · 1.58 KB
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
import os
BOOKS_DIR = "./"
README_FILE = "README.md"
EXCLUDE_DIRS = {".git", ".github", "__pycache__"} # 可以扩展过滤目录
EXCLUDE_FILES = {".DS_Store", "Thumbs.db"} # 可以扩展过滤文件
def scan_books(base_dir=BOOKS_DIR):
"""递归扫描目录,生成 Markdown 列表"""
lines = []
for root, dirs, files in os.walk(base_dir):
# 过滤不需要的目录
dirs[:] = [d for d in dirs if d not in EXCLUDE_DIRS and not d.startswith(".")]
rel_dir = os.path.relpath(root, base_dir)
indent_level = 0 if rel_dir == "." else rel_dir.count(os.sep) + 1
indent = " " * indent_level
# 如果是子目录,加目录标题
if rel_dir != ".":
folder_name = os.path.basename(root)
lines.append(f"{indent}- **{folder_name}/**")
# 文件列表
for file in sorted(files):
if file in EXCLUDE_FILES or file.startswith("."):
continue
file_path = os.path.join(root, file).replace("\\", "/")
display_name = file
lines.append(f"{indent} - [{display_name}]({file_path})")
return lines
def update_readme():
"""更新 README.md"""
books_list = scan_books()
header = "# 📚 我的电子书收藏\n\n以下是仓库中的电子书:\n\n"
content = "\n".join(books_list) if books_list else "(目前还没有电子书哦)"
with open(README_FILE, "w", encoding="utf-8") as f:
f.write(header + content + "\n")
print(f"✅ 已更新 {README_FILE}")
if __name__ == "__main__":
update_readme()