Skip to content

Instantly share code, notes, and snippets.

@bsag
Created June 8, 2013 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bsag/5734883 to your computer and use it in GitHub Desktop.
Save bsag/5734883 to your computer and use it in GitHub Desktop.
Quick and dirty script to parse slug from filename and add to file for Pelican
#!/usr/bin/env python
import os
import os.path
import re
path = "."
for file in os.listdir(path):
current_file = os.path.join(path, file)
if os.path.isfile(current_file):
current_name = os.path.basename(current_file)
m = re.search('(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)\.md', current_name)
slug = m.group('slug')
first_line = "Slug: " + slug + "\n"
# first read existing file in and save lines
f_old = open(current_file, 'r')
temp = f_old.read()
f_old.close
# then write the lines back out to the same file, prepending slug line
f_new = open(current_file, 'w')
f_new.write(first_line)
f_new.write(temp)
f_new.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment