[New-bugs-announce] [issue11344] Add height argument to os.path.dirname()

blokeley report at bugs.python.org
Sun Feb 27 09:26:48 CET 2011

New submission from blokeley <blokeley at gmail.com>:

It is a common need to find the grandparent or great-grandparent (etc.) of a given directory, which results in this:

>>> from os.path import dirname
>>> mydir = dirname(dirname(dirname(path)))

Could a "height" parameter be added to os.path.dirname so it becomes:

>>> def dirname(path, height=1):

Then we could have usage like:

>>> path = '/ggparent/gparent/parent/myfile.txt'
>>> from os.path import dirname

>>> dirname(path)

>>> dirname(path, 2)

>>> dirname(path, 3)

Perhaps we should throw ValueErrors for invalid height values:

>>> dirname(path, 10)

>>> dirname(path, -1)

Perhaps a height of 0 should do nothing:

>>> dirname(path, 0)

I can supply patches, unit tests and docs if you like.

components: Library (Lib)
messages: 129616
nosy: blokeley
priority: normal
severity: normal
status: open
title: Add height argument to os.path.dirname()
type: feature request
versions: Python 3.3

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list