目錄python
#!/usr/bin/env python # -*- coding:utf-8 -*- 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文件而且複製一份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]])) # 打開md文件而後進行替換 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('\(/配圖/', '(配圖/', data) # data = re.sub('<br>', '<br>\n', data) # data = re.sub('<br>', '', data) data = re.sub('^ #.*? ', '', data) fw.write(data) # 新文件一次性寫入原文件內容 # fw.flush() # 刪除原文件 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)