If you use jquery and haven’t messed with the jqueryFileTree plugin, I’d highly recommend you do so. It’s pretty neat, and there are connector scripts already written in various languages for use with this plugin. However, there wasn’t one for python cgi (there was one for django however). So, I wrote my own! Try it out:
#!/usr/bin/python
# jqueryFileTree connector script for python cgi
# Version: 1.0 / 10 August 2010
# Author: Charles Hamilton / musashi@nefaria.com
# Released under the GNU GPLv3
# Modifications and improvements are welcome
import os, cgi, cgitb, urllib, re
cgitb.enable()
form = cgi.FieldStorage()
print 'Content-Type: text/html\n\n'
print '<ul class="jqueryFileTree" style="display: none;">'
path = urllib.unquote(form['dir'].value)
dirs = []
files = []
filelist = sorted(os.listdir(path))
for object in filelist:
if os.path.isfile(path + '/' + object):
ext = os.path.splitext(object)
files.append('<li class="file ext_' + re.sub('\.', '', ext[1]) + '"><a href="#" rel="'+ path + object + '">'+ object + '</a></li>')
elif os.path.isdir(path + '/' + object):
dirs.append('<li class="directory collapsed"><a href="#" rel="'+ path + object +'/">' + object + '</a></li>')
for d in dirs:
print d
for f in files:
print f
print '</ul>'