当图床迁移后 , 我们需要将之前的 Hexo 文章中的图片链接换为新的地址 . 这里写一个批量文件处理小程序来解决
这里使用 python 实现
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
|
import os import re import time
def modify_md_content(top): for root, dirs, files in os.walk(top, topdown=False): for file_name in files: file_name_split = file_name.split('.')
try: if file_name_split[-1] == 'md': md_file_path = os.path.join(root, '.'.join(file_name_split)) copy_md_file_path = os.path.join(root, '.'.join([f'{file_name_split[0]}_copy', file_name_split[1]]))
with open(md_file_path, 'r', encoding='utf8') as fr, \ open(copy_md_file_path, 'w', encoding='utf8') as fw: data = fr.read() data = re.sub('^[TOC] #.*? ', '', data)
fw.write(data)
os.remove(md_file_path) os.rename(copy_md_file_path, md_file_path) print(f'{md_file_path} done...') time.sleep(0.5) except FileNotFoundError as e: print(e) time.sleep(0.5)
if __name__ == '__main__': top = r'/Users/mac/Desktop/' modify_md_content(top)
|
参考链接