批量给md文件写入内容
2021-09-30 本文已影响0人
望月成三人
- 迁移博客到hexo,需要把md文件批量处理,头部批量加入内容,代码如下
import os
path = r"D:\book\user-2231755-1632815565\python"
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
_path = os.path.join(root, name)
_name = name[:len(name)-3]
with open(_path, "r+", encoding="utf-8") as f:
old = f.read()
f.seek(0)
f.write("---\n")
f.write("title: %s\n" %_name)
f.write("date: 2021-09-29 17:13:35\n")
f.write("categories: python\n")
f.write("---\n")