By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,222 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,222 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Mar 14 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Thomas Ploch a écrit :
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.
Let me guess : your cgi script is running on *n*x ?-)
Is there an easy way around this,
Probably.
>>fnames = "C:\\dir\\data.ext", "/dir/data.txt", "dir:data"
import ntpath, posixpath, macpath
def basename(filename):
.... for m in ntpath, posixpath, macpath:
.... if m.sep in filename:
.... return m.basename(filename)
.... else:
.... # XXX
.... raise SomeException('could not do the job')
....
>>for f in fnames:
.... print f, basename(f)
....
C:\dir\data.ext data.ext
/dir/data.txt data.txt
dir:data data
>>>
Might need some improvements, but you get the idea...

HTH
Mar 14 '07 #2

P: n/a
Bruno Desthuilliers schrieb:
>
Let me guess : your cgi script is running on *n*x ?-)
Pretty hard to get this one, heh? :-D
>
Probably.
Good that you decided I was worth the information.
>>fnames = "C:\\dir\\data.ext", "/dir/data.txt", "dir:data"
>>import ntpath, posixpath, macpath
>>def basename(filename):
... for m in ntpath, posixpath, macpath:
... if m.sep in filename:
... return m.basename(filename)
... else:
... # XXX
... raise SomeException('could not do the job')
...
>>for f in fnames:
... print f, basename(f)
...
C:\dir\data.ext data.ext
/dir/data.txt data.txt
dir:data data
>>>
Thnaks a lot. :)

Thomas

Mar 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.