walktree browser filenames problem

D

dimitri pater

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 &amp;
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')
 
R

Richard Brodie

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).

urllib.quote() should do the trick. You need to follow the rules for
encoding URIs as well as the HTML ones.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top