if __name__ == '__main__': cate, tags = Counter(), Counter() for dirpath, dirnames, filenames in os.walk("./source/_posts"): for filename in filenames: if filename.endswith('.md'): st = list() withopen(os.path.join(dirpath, filename), 'rt+', encoding='utf-8') as f: state = 0 for line in f: if state == 1and'---'in line: break if"categories"in line: state = 1 if state == 1: st.append(line) *categories, tag = st iflen(categories) == 1: category = [categories[0].split(':')[1].strip()] else: category = map((lambda x: x.strip()[3:-1]), categories[1:]) tag = tag.strip()[7:-1].split(',') for c in category: cate[c] += 1 for t in tag: tags[t] += 1 print("Categories:") for k, v in cate.items(): print(f"- {k}: {v}") print() print("Tags:") for k, v in tags.items(): print(f"- {k}: {v}")