File: class/Workbook/Examples/Lecture10/site-forward.py
####################################################### # Create forward link pages for relocating a web site. # Generates one page for every existing site file; # upload the generated files to your old web site. # Performance note: the first 2 string.replace calls # could be moved out of the for loop, but this runs # in < 1 second on my Win98 machine for 150 site files. # Note: the os.listdir call can be replaces with: # sitefiles = glob.glob(sitefilesdir + os.sep + '*') # but then the file/directory names must be split: # dirname, filename = os.path.split(sitefile) # see site-forward-glob.py for this implementation. ####################################################### import os, string uploaddir = 'rmi-forward' # where to store forward files servername = 'starship.python.net' # where site is relocating to homedir = '~lutz' # where site will be rooted sitefilesdir = 'public_html' # where site files live locally templatename = 'template.html' # template for generated pages template = open(templatename).read() sitefiles = os.listdir(sitefilesdir) # filenames, no directory prefix #print sitefiles count = 0 for filename in sitefiles: fwdname = os.path.join(uploaddir, filename) # or + os.sep + filename print 'creating', filename, 'as', fwdname filetext = string.replace(template, '$server$', servername) # insert text filetext = string.replace(filetext, '$home$', homedir) # and write filetext = string.replace(filetext, '$file$', filename) # file varies open(fwdname, 'w').write(filetext) count = count + 1 print 'Last file =>\n', filetext print 'Done:', count, 'forward files created.'