Created
March 5, 2022 04:56
-
-
Save yoonbae81/6a526e9553e90d48484a85561ae2fab0 to your computer and use it in GitHub Desktop.
A scriptlet for apply template to multiple files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from datetime import datetime | |
files = [f for f in Path().iterdir() if f.suffix == '.md'] | |
files | |
template = Path('template').read_text() | |
template | |
def apply(source, template): | |
output = template | |
dt = datetime.fromtimestamp(source.stat().st_mtime).strftime('%Y%m%d%H%M') | |
output = template.replace('[[DATETIME]]', dt) | |
txt = source.read_text() | |
output = output.replace('[[CONTENTS]]', txt) | |
return output | |
for f in files: | |
output = apply(f, template) | |
Path('output/' + f.name).write_text(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment