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

Thomas Ploch Thomas.Ploch at gmx.net
Wed Mar 14 22:15:09 CET 2007


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




More information about the Python-list mailing list