forked from xdite/note-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_image.py
More file actions
23 lines (18 loc) · 845 Bytes
/
Copy pathreplace_image.py
File metadata and controls
23 lines (18 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
import re
# 修改此處為您的目錄路徑
directory_path = '.'
# 遍歷指定的目錄中的所有 .md 檔
for folder_name, subfolders, filenames in os.walk(directory_path):
for filename in filenames:
if filename.endswith('.md'):
file_path = os.path.join(folder_name, filename)
# 讀取文件內容
with open(file_path, 'r', encoding='utf-8') as file:
file_contents = file.read()
# 用正則表達式替換字符串
updated_contents = re.sub(r'!\[\[Pasted image (.+?).png\]\]', r'', file_contents)
# 如果文件被更改,則寫回文件
if updated_contents != file_contents:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(updated_contents)