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

Steve Holden steve at holdenweb.com
Wed Mar 14 15:07:09 EDT 2007


Thomas Ploch wrote:
> 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

First of all you should clarify your thinking about this issue. The 
problem isn't with os.path.basename (which assumes that paths are given 
for the local filestore architecture), the problem is that you are 
failing to correctly pre-condition your data. It's nothing to do with 
Python escapes - they are only relevant when you are writing string 
literals in Python. Pay no attention to the way the strings are printed 
- the Windows client is sending you a single backslash character for 
each path separator in its filename. You can check this using the len() 
function on the returned string.

Clearly if form['uploadfile'] is returning the client's path information 
you do have to remove that somehow before further processing, which also 
means you need to deduce what the client architecture is to correctly 
remove path data. Of course this also leaves open the question "what 
does a Mac client return?", and you might want to cater for that also.

I suspect you will also find that there are at least some circumstances 
under which a Unix browser will also include path information.

I presume you are looking to use the same filename that the user 
provided on the client? Does this mean that each user's files are 
stored in different directories? Otherwise it's not always a good idea 
to use filenames provided by the user for files on the server anyway.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Blog of Note:          http://holdenweb.blogspot.com
See you at PyCon?         http://us.pycon.org/TX2007




More information about the Python-list mailing list