Python批处理文档为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
import os, time

path = r"./" #采用相对路径读取当前目录md文件

for root, dirs, files in os.walk(path, topdown=False):
for name in files:
if not ".md" in name: continue
print(name)
_path = os.path.join(root, name)
#editTime = time.strptime(os.path.getmtime(_path), "%Y-%m-%d %H:%M:%S")
editTime = time.localtime(os.path.getmtime(_path)) #获取文档最后修改时间
editTime = time.strftime("%Y-%m-%d %H:%M:%S", editTime) #格式化时间戳
_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: %s\n" %editTime)
f.write("categories:\n")
f.write("tags:\n")
f.write("---\n")
f.write(old) #将文件原内容追加到前缀后面
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2024 Konsin
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信