问题分析 事情是这样的
我原来的博客的日期是在文件名中的,而新的模板中的日期是放到了md文件文件头中
我这几百篇博客当然不能手动修改了,于是小编就利用了一个python中的文件操作和re模块写了一个脚本实现了这个替换功能
代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 ''' #!/usr/bin/env python # -*- coding:utf-8 -*- # Created by victor # Created Time: '2021/3/6 21:05' ''' import osimport re""" --- title: "Flask 概述" cover: "/img/lynk/93.jpg" date: 2021-03-06 tags: - Python - solution - web - flask --- """ """ --- layout: keynote title: "Example Post using Keynote Layout" cover: "/img/lynk/93.jpg" date: 2021-03-06 subtitle: "Keynote: JavaScript Modularization Journey" iframe: "http://huangxuan.me/js-module-7day/" date: 2015-07-09 author: "Hux" header-img: "post-bg-js-version.jpg" tags: - 前端开发 - JavaScript --- """ def alter (file,old_str,new_str ): newName = re.sub(r'\d\d\d\d-\d\d-\d\d-' , "" , file) with open (file, "r" , encoding="utf-8" ) as f1,open (newName, "w" , encoding="utf-8" ) as f2: for line in f1: print("old_str>>>" ,old_str) print("new_str>>>" ,new_str) print("line>>>" ,line) print("==================" ) f2.write(line) if line[0 :6 ] == "title:" : f2.write(new_str) os.remove(file) def movDate (dir ): newDir = dir if os.path.isfile(dir ): if dir [-3 :] == ".md" : searchObj = re.search('\d\d\d\d-\d\d-\d\d-' , dir ) if searchObj: print(dir + "是日期开头的md文件" ) date_str = searchObj.group(0 )[:-1 ] app_str = "date: " +date_str+"\n" alter(dir , "\ntitle:" , app_str) elif os.path.isdir(dir ): print(dir + "是文件夹" ) for s in os.listdir(dir ): newDir = os.path.join(dir , s) movDate(newDir) movDate('E:\\Projects\\PycharmProjects\\untitled\\test\\database\\mysql4' ) ''' ┌─┐ ┌─┐ + + ┌──┘ ┴───────┘ ┴──┐++ │ │ │ ─── │++ + + + ███████───███████ │+ │ │+ │ ─┴─ │ │ │ └───┐ ┌───┘ │ │ │ │ + + │ │ │ └──────────────┐ │ │ │ ├─┐ │ ┌─┘ │ │ └─┐ ┐ ┌───────┬──┐ ┌──┘ + + + + │ ─┤ ─┤ │ ─┤ ─┤ └──┴──┘ └──┴──┘ + + + + 神兽保佑 代码无BUG! '''
参考