walktree browser filenames problem

Discussion in 'Python' started by dimitri pater, Feb 4, 2005.

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

    --
    Please visit dimitri's website: www.serpia.com
    dimitri pater, Feb 4, 2005
    #1
    1. Advertising

  2. "dimitri pater" <> wrote in message
    news:...

    > 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.
    Richard Brodie, Feb 4, 2005
    #2
    1. Advertising

  3. thanks!
    now it works:
    ret.append(' <tr><td class="file"><a
    href=http://e-bench.serpia.com/upload/%s>%s</a></td></tr>\n' %
    (urllib.quote(escape(name)),escape(name)))

    bye,
    Dimitri


    On Fri, 4 Feb 2005 11:45:17 -0000, Richard Brodie <> wrote:
    >
    > "dimitri pater" <> wrote in message
    > news:...
    >
    > > 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.
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    Please visit dimitri's website: www.serpia.com
    dimitri pater, Feb 4, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. moxie
    Replies:
    0
    Views:
    988
    moxie
    Jul 22, 2003
  2. =?Utf-8?B?SW5jb3JyZWN0IGZpbGVuYW1lcyBpbiBkb3dubG9h

    Problem with filenames in response.addheader()

    =?Utf-8?B?SW5jb3JyZWN0IGZpbGVuYW1lcyBpbiBkb3dubG9h, Oct 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    539
    Rob Meade
    Oct 28, 2005
  3. B.J.
    Replies:
    4
    Views:
    741
    Toby Inkster
    Apr 23, 2005
  4. David Squire
    Replies:
    9
    Views:
    130
    David Squire
    Sep 27, 2006
  5. Hemant

    Problem expanding filenames in loop

    Hemant, Jun 23, 2008, in forum: Perl Misc
    Replies:
    13
    Views:
    155
    Ben Morrow
    Jun 24, 2008
Loading...

Share This Page