os.path.basename() - only Windows OR *nix?

Thomas Ploch Thomas.Ploch at gmx.net
Wed Mar 14 14:49:17 EDT 2007


Hello,

I have a cgi script that handles fileuploads from windows and *nix
machines. i need os.path.basename(filename) to get the pure filename.

For *nix, thats not a problem, but for windows, it always returns the
full path:



#/usr/bin/env python

import cgi, os
import cgitb; cgitb.enable()

form = cgi.FieldStorage()
filename = os.path.basename(form['uploadfile'].filename)

print 'Content-Type: text/html\n\n'
print filename



-----------------

For 'C:\text\text.txt', the output is 'C:\text\text.txt', which should
be 'text.txt', the same happens for 'C:\\text\\text.txt'. I think its
the escapes that makes python not splitting it. All Unix style paths get
converted the right way.

Is there an easy way around this, or do I really have to write a parser
including _all_ Python escapes?

Thomas



More information about the Python-list mailing list