[Python-Dev] os.path.dirname misleading?

Skip Montanaro skip@pobox.com
Wed, 12 Mar 2003 10:54:57 -0600


    Kevin> However, to get what I would consider correct behavior based on
    Kevin> the function name, the code would need to be:

    Kevin> def dirname(p):
    Kevin>     """Returns the directory component of a pathname"""
    Kevin>     if isdir(p):
    Kevin>         return p
    Kevin>     else:
    Kevin>         return split(p)[0]

No can do.  On my Mac I could execute:

    >>> import ntpath
    >>> print ntpath.dirname("C:\\system\\win32")
    C:\system

Calling isdir() is not an option.

Taken another way, "/usr/bin" is a path to a file, so "/usr" is its
directory component.  and "bin" is its basename:

    >>> os.path.dirname("/usr/bin")
    '/usr'
    >>> os.path.basename("/usr/bin")
    'bin'

That "/usr/bin" happens to also be a directory is beside the point.

Skip