walktree browser filenames problem

dimitri pater dimitri.pater at gmail.com
Fri Feb 4 06:30:17 EST 2005


Hello,

I use the following script to list the files and download files from
my website (99% of the code is from
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/200131).
The problem is that the filenames are cut off in the status bar of the
browser because of the white space (eg 'hello.pdf' works, but 'hello
there.pdf' is displayed as hello).
The filenames are displayed correctly in the main page of the browser.
I tried several things, maybe somebody knows how to do it?

script: (sorry for the long list)

#! /usr/bin/python

import os, sys
import cgi
print "Content-type: text/html"
print
print "<pre>"

try:
    import cgitb
    cgitb.enable()
except:
    sys.stderr = sys.stdout

print "</pre>"

def walktree(top = "../upload", depthfirst = True):
    import os, stat, types
    names = os.listdir(top)
    if not depthfirst:
        yield top, names
    for name in names:
        try:
            st = os.lstat(os.path.join(top, name))
        except os.error:
            continue
        if stat.S_ISDIR(st.st_mode):
            for (newtop, children) in walktree (os.path.join(top,
name), depthfirst):
                yield newtop, children
    if depthfirst:
        yield top, names

def makeHTMLtable(top, depthfirst=False):
    from xml.sax.saxutils import escape # To quote out things like &
    ret = ['<table class="fileList">\n']
    for top, names in walktree(top):
        ret.append('   <tr><td class="directory">%s</td></tr>\n'%escape(top))
        for name in names:
            ret.append('   <tr><td class="file"><a
href=http://e-bench.serpia.com/upload/%s>%s</a></td></tr>\n' %
(escape(name),escape(name)))
    ret.append('</table>')
    return ''.join(ret) # Much faster than += method

def makeHTMLpage(top, depthfirst=False):
    return '\n'.join(['<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"',
                      '"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
                      '<html>'
                      '<head>',
                      '   <title>Search results</title>',
                      '   <style type="text/css">',
                      '      table.fileList { text-align: left; }',
                      '      td.directory { font-weight: bold; }',
                      '      td.file { padding-left: 4em; }',
                      '   </style>',
                      '</head>',
                      '<body>',
                      '<h1>Documenten e-bench</h1>',
                      makeHTMLtable(top, depthfirst),
                      '<BR><BR><HR><B><A
HREF="http://e-bench.serpia.com">Home</A></B>',
                      '<h5>To do: escape chars verwijderen</h5>',
                      '<h5>...</h5>',
                      '<h5>Aparte HTML template maken als in upload.py</h5>',
                      '</body>',
                      '</html>'])
                   
if __name__ == '__main__':
    if len(sys.argv) == 2:
        top = sys.argv[1]
    else: top = '.'
    print makeHTMLpage('../upload')

-- 
Please visit dimitri's website: www.serpia.com



More information about the Python-list mailing list