473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
2 4865
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Coder Droid | last post by:
After much searching, I can't seem to find any definitive way to determine which path separator to use. Obviously, it's ":" on nix, and ";" on windows ... so what's the best way to determine what...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
0
by: praba kar | last post by:
Dear All, I am using Python 2.4 in the linux platform. At present I am working on webbased file manager. When I try to upload a file from the linux cgi script easily fetch the file name by the...
12
by: Xah Lee | last post by:
Python Doc Problem Example Quote from: http://docs.python.org/lib/module-os.path.html ---------- split( path) Split the pathname path into a pair, (head, tail) where tail is the last...
3
by: Xah Lee | last post by:
Split File Fullpath Into Parts Xah Lee, 20051016 Often, we are given a file fullpath and we need to split it into the directory name and file name. The file name is often split into a core...
34
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH...
18
by: Aaron Gray | last post by:
Has anyone got a code snippet to separate out the path components, ie drive, path, filename, and extension ? Many thanks in advance, Aaron
9
by: mathieu | last post by:
I'd like to be able to get the path to the current working executable (from inside it). Technically this is easy, I simply have to collapse: getcwd and argv Well argv comes in a little late,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.