博客要从gridea改成hexo的,但是要涉及到好多的图片显示,hexo又不支持相对路径,这让我很困扰,索性尽量少些图片的文章,但是之前那些还要去更改。一个个手动改太过于麻烦,所以写了个python脚本用于查询。 ```python import os import re # 常见图片格式 IMAGE_PATTERN = re.compile(r'!\[.*?\]\((.*?\.(?:png|jpe?g|gif|bmp|svg))\)', re.IGNORECASE) # 路径 base_path = r"D:\oneDrive\Note\blog\source" def find_md_with_images(base_path): md_files_with_images = [] for root, _, files in os.walk(base_path): for file in files: if file.endswith('.md'): file_path = os.path.join(root, file) # 检查文件内容是否包含图片 with open(file_path, 'r', encoding='utf-8') as f: content = f.read() if IMAGE_PATTERN.search(content): md_files_with_images.append(file_path) return md_files_with_images # 查找并打印结果 if __name__ == "__main__": result = find_md_with_images(base_path) if result: print(f"包含图片的Markdown文件({len(result)}个):") for md_file in result: print(md_file) else: print("未找到包含图片的Markdown文件。") ``` Loading... 博客要从gridea改成hexo的,但是要涉及到好多的图片显示,hexo又不支持相对路径,这让我很困扰,索性尽量少些图片的文章,但是之前那些还要去更改。一个个手动改太过于麻烦,所以写了个python脚本用于查询。 ```python import os import re # 常见图片格式 IMAGE_PATTERN = re.compile(r'!\[.*?\]\((.*?\.(?:png|jpe?g|gif|bmp|svg))\)', re.IGNORECASE) # 路径 base_path = r"D:\oneDrive\Note\blog\source" def find_md_with_images(base_path): md_files_with_images = [] for root, _, files in os.walk(base_path): for file in files: if file.endswith('.md'): file_path = os.path.join(root, file) # 检查文件内容是否包含图片 with open(file_path, 'r', encoding='utf-8') as f: content = f.read() if IMAGE_PATTERN.search(content): md_files_with_images.append(file_path) return md_files_with_images # 查找并打印结果 if __name__ == "__main__": result = find_md_with_images(base_path) if result: print(f"包含图片的Markdown文件({len(result)}个):") for md_file in result: print(md_file) else: print("未找到包含图片的Markdown文件。") ``` 最后修改:2024 年 11 月 20 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏