NEWbie asks "is Python a good tool for automated file/folder generation?"
Chris Barker
Chris.Barker at noaa.gov
Mon Jun 17 16:55:43 EDT 2002
Unconditional Grace wrote:
> What I would like to do is largely string-based file and folder
> manipulation:
you've already gotten a couple of suggestions for some partsw of your project.
> 3. Within each folder, create one HTML page consisting of a table
> with a link to each image, preferably with a thumbnail (can be
> original image resized by HTML since this is all local = no bandwidth
> issues).
I can do you one better. Following is a scripit I wrote that does
something similar. It takes a set of directories with jpegs in them, and
for each one creates an html page with a set of thumbnails of the
images, and links to the images. I used the Python Imaging Library (PIL)
to make the thumbnails, and HTMLGen to help generate the HTML. You can
find both of these packages by doing a google search on the web. There
are probably a lot of other options for creating htm thatn HTMLgen, I
havn't looked recently. You will find that one of the stregths of Python
is the available modules, both the "standard" modules, and a huge
assortment of others. Use them. If you are doing something that seems
like a common task, there is probably a module out there to help you.
> 4. Create a top level HTML page that links to all 100 image HTML
> pages created in step 3. Can be text only.
The top level page links to the thumbnail pages, but it could just as
easily have a full set of links.
> 5. Optional: in step 3, make each image part of an HTML page with
> "previous image" and "next image" links, rather than just being called
> by the browser as a jpg image.
Exercise left for the reader.
> I've never used Python, and I'm wondering if you, collectively, think
> that Python is a good language to do this.
It's a fabulous tool for this sort of thing.
> While it seems like Python
> is an incredibly useful tool, I'm not sure I have time right now to
> invest in learning it if it can't do this.
It wouldn't be such a useful tool if it couldn't do this!! It is well
worth taking the time to learn.
> It seems I frequently want
> to do repetitive Windows file/folder chores like this, so I want to
> get smart-like.
Good idea. In this case, you are manipulating files/folder, building
html, and doing image manipulation (if you want the thumbnails) Python
is an excellent tool for all these tasks.
> I'm not asking anyone to write such code for me, heh heh,
sorry, I already did...
good luck,
-Chris
The Code: note that it was written in Python 1.5, a couple of years ago.
If I were to re-write it now, I would make some changes. It needs PIL
and HTMgen
#!/usr/bin/python
# This is a Python script that builds a bunch of web pages out of a set
# of directories with pictures in them
import Image, glob, os
import HTMLgen
HT = HTMLgen
main_name = "index.html"
num_columns = 4 # number of columns for thumbnail table
maindir = os.getcwd()
maindirname = os.path.split(maindir)[1]
maindoc = HT.SimpleDocument(title=maindirname)
maindoc.append(HT.Heading(1,maindirname))
dirlist = filter(lambda name: os.path.isdir(name),os.listdir(os.getcwd()))
for dirname in dirlist:
os.chdir(os.path.join(maindir,dirname))
doc = HT.SimpleDocument(title=dirname)
doc.append(HT.Heading(1,dirname))
table = HT.Table('Click on the small images to see them full size')
table.width = ''
doc.append(table)
table.body = []
count = 0
picts = glob.glob("*.jpg")
picts = filter(lambda name:(not (name[-10:] == '.thumb.jpg')),picts)
while count < len(picts):
row = ['']*num_columns
for i in range(num_columns):
count = count+1
if count > len(picts):break
pict = picts[count-1]
im = Image.open(pict)
im.thumbnail((128,128))
im.save((pict[:-3]+'thumb.jpg'),'JPEG')
row[i] = (HT.Href(pict,HT.Image(pict[:-3]+'thumb.jpg')))
table.body.append(row)
doc.append(HT.BR())
doc.append(HT.Href(os.path.join(os.pardir,"index.html"),"Back to Main
Index"))
doc.write("index.html")
maindoc.append(HT.BR())
maindoc.append(HT.Heading(2,HT.Href(os.path.join(dirname,"index.html"),dirname)))
os.chdir(maindir)
maindoc.append(HT.BR())
maindoc.append(HT.Href("http://www.python.org",HT.Image("PythonPoweredAnim.gif")))
maindoc.write('index.html')
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the Python-list
mailing list