Skip to content

Instantly share code, notes, and snippets.

@yoonbae81
Created March 5, 2022 04:56
Show Gist options
  • Save yoonbae81/6a526e9553e90d48484a85561ae2fab0 to your computer and use it in GitHub Desktop.
Save yoonbae81/6a526e9553e90d48484a85561ae2fab0 to your computer and use it in GitHub Desktop.
A scriptlet for apply template to multiple files
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