Created
October 26, 2023 05:58
-
-
Save lixingcong/ba0719145f34ee702de9e461af4e9550 to your computer and use it in GitHub Desktop.
从Notion导出的markdown删掉第一行的同名标题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
# /tmp/1.txt获取方式 | |
# find /path/to/dir -type f -name '*.md' > /tmp/1.txt | |
with open("/tmp/1.txt", 'r') as mdList: | |
for md in mdList: | |
filepath = md.strip() | |
filename = Path(md).stem | |
#print(filename) | |
lines = [] | |
with open(filepath, 'r', encoding='utf-8') as f: | |
lines = f.readlines() | |
if len(lines) > 0: | |
firstline = lines[0].strip() | |
if firstline == '# ' + filename: | |
# 默认删除第一行 | |
offset = 1 | |
print(filename) | |
# 第二行为空行 | |
if len(lines) > 1 and lines[1].strip() == '': | |
offset += 1 | |
with open(filepath, 'w+', encoding='utf-8') as f: | |
f.writelines(lines[offset:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment