Images to HTML

I’m new to programming, looking to learn Python. Is this kind of task a good one for Python? I’d like to learn Python anyhow.

I have a directory of images…


20080105_123932.jpg
20080111_203500.jpg
20080113_114714.jpg

And I’d like to parse them into structured HTML:


<img src="../images/2008/20080105_123932.jpg" /><br />
Saturday 1/5 12:39pm<br /><br /><br />
<img src="../images/2008/20080111_203500.jpg" /><br />
Friday 1/11 8:35pm<br /><br /><br />
<img src="../images/2008/20080113_114714.jpg" /><br />
Sunday 1/13 11:47am<br /><br /><br />

I’m wondering if Python is the right choice… does it have a function that parses proper day names & times from a naming structure like yyyymmdd_hhmmss ? Or should I write my own?

Thanks for any pointers.

Python would be great for this. Using the datetime module you can convert strings into datetime objects then back again. Try this out.

import glob
import os
import datetime
 
# Start with empty string
html = ''
 
# Get all JPG filenames from certain folder
fileNames = glob.glob(r'C:	emp\eric\*.jpg')
 
# For each filename create formatted HTML string
for fname in fileNames:
   # Get base filename string
   timeStr = os.path.splitext(os.path.basename(fname))[0]
 
   # Parse it into a datetime object
   timeObj = datetime.datetime.strptime(timeStr, '%Y%m%d_%H%M%S')
 
   # Use that to make new formatted string
   fullTimeStr = timeObj.strftime('%A %m/%d %H:%M:%S%p')
   # Make row string for this file, with new format
   rowStr = '<img src="../images/2008/%s.jpg" /><br />
%s<br /><br /><br />
' % (timeStr, fullTimeStr)
 
   # Append to running HTML string
   html += rowStr
 
# Write HTML to file
outFile = open(r'C:	emp\myHTML.html', 'w')
outFile.write(html)
outFile.close()
 
# Also print results
print html

Hey thanks Adam, I’ll give it a whirl. Nice to have something to dissect too!