the mystery of dirname()

MRAB python at
Sat Feb 20 18:49:14 CET 2010

Shashwat Anand wrote:
> basically I infer that : dirname = path - basename, like for path =  
> '//x', basename = x, hence dirname = '//'
Basically, os.path.dirname() should return the directory name, which
means dropping everything after the last slash, and also the last slash.
However, there's the special case where the name is in the root
directory, and you want to retain the slash(es).

When building a path (os.path.join()) you want to join the parts with a
single slash between them, but there's the special case when a part is
the root directory, and you don't want to add a slash between them, eg
os.part.join('/x', 'y') should return '/x/y', but os.part.join('/', 'x')
should return '/x'.

More information about the Python-list mailing list