# 遍历_posts目录及其子目录下的所有文件 for root, dirs, files in os.walk(posts_dir): dirs.sort() for file insorted(files): file_path = os.path.join(root, file) file_path = os.path.abspath(file_path) ifnot file_path.endswith(".md"): continue withopen(file_path, 'r') as r_file: lines = r_file.readlines() for line in lines: if line.startswith("updated"): timestr = line.replace("updated: ", "").strip() date_time = time.strptime(timestr, "%Y-%m-%d %H:%M:%S") touch_str = time.strftime("%Y%m%d%H%M", date_time) touch_cmd = "touch -mt " + touch_str + " " + file_path ls_cmd = "ls -l " + file_path os.system(touch_cmd) print(touch_str, "has been forced added to", file_path, "Result:") os.system(ls_cmd)
检查crc是否有重复
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import os from collections import Counter # 找出crc32重复元素 posts_dir = "source/_posts" for root, dirs, files in os.walk(posts_dir): dirs.sort() for file insorted(files): filePath = os.path.join(root, file) filePath = os.path.abspath(filePath) ifnot filePath.endswith(".md"): continue withopen(filePath, "r") as file: lines = file.readlines() for line in lines: if line.startswith("addrlink: "): addrlinklist.append(line.replace("addrlink: ", "").strip()) break b = dict(Counter(addrlinklist)) print({key:value for key,value in b.items() if value > 1})