I wanted to revisit this based on my experiences. The problem was cleaning filenames from file uploads in browers that pass the entire local path to the server. Stupid browsers. os.path.basename seems to only work in the context of the path as given on the local system: ie. when used on Unix, it does not fix up Windows filenames. At the very least I can't get it to work on my Mac OS X box. Terry's suggestion again neglects non-Windows paths where forward slash or semicolon can be the separator. I'm left thinking my algorithm - find the last slash, backslash, or semicolon, and go to the end - is the most universal approach. Although I'm entirely reluctant to come to the feet of the masters and contradict them ;-) Ed On Friday, September 19, 2003, at 06:33 PM, Terry Hancock wrote:
On Friday 19 September 2003 05:16 pm, Edward Pollard wrote:
For the sake of completeness: filename[max(string.rfind(filename, '/'),string.rfind(filename, '\\'),string.rfind(filename, ':'),)+1:]
str(filename).split('\\')[-1]
On Friday, September 19, 2003, at 04:44 PM, Paul Winkler wrote:
Or you could let the python core developers do it for you:
os.path.basename(foo)