Created
June 8, 2013 11:29
-
-
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
This file contains hidden or 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
#!/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