Newbie: Is there a more pythonic approach?

Lloyd Sommerer lsommerer at sewardweb.com
Tue Mar 13 15:49:44 EST 2001


I'm trying to reduce 404 errors (at least one that visitors see) on my
website by correcting problems with case and extensions on the fly.
This is one of my first python programs, and I was wondering if it:

a) made use of python language features in a way that seems natural to
"real" python programmers.
b) missed any language construction that makes the program "better"
(faster, easier to read...)

Thanks, the program follows below.

Lloyd Sommerer
=========================================
error_page = '/404.html'
base_url = 'http://churchsite.gatheringspot.com'
base_directory = '/home/gatheri/public_html/churchsite/'
extension_list = ['.html','.shtml','.htm','.txt','.php','.asp']

import string
import os

lowercase_url = string.lower(os.environ['REDIRECT_URL'])
url_path,url_extension = os.path.splitext(lowercase_url)
new_url = base_url+error_page
#
# Check to see if simply converting the URL to lowercase corrects the
problem.
# If it doesn't, check to see if the extension is incorrect and fix it
if it is.
#
if os.path.exists(base_directory+lowercase_url):
    new_url = base_url+lowercase_url
else:
    for extension in extension_list:
        if os.path.exists(base_directory+url_path+extension):
            new_url = base_url+url_path+extension
#
# Send HTTP header information to the browser so that it can request the
"correct" document.
#
print "Status: 301 Permanently moved"
print "Location: " + new_url
print




More information about the Python-list mailing list