Automate creating files reading a text file containing filenames using Python.

I had a text file with blog post titles and filenames. I use a template to create blog posts. When I create only one blog post I just cp template _posts/ but now I had more than 10 posts to create.

The text file looked like this:

An awesome blog post...

Some other blog post


Yet another maybe 15th blog post

Use shutil.copy2 to copy the template into the new file.

import shutil

with open('posts.txt', 'r') as fh:
    for line in fh:
	    if '.md' in line:
		    shutil.copy2('template', line.rstrip('\r\n'))

Ask me anything on Linkedin