This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author blokeley
Recipients blokeley
Date 2011-02-27.08:26:48
SpamBayes Score 4.137609e-09
Marked as misclassified No
Message-id <1298795208.87.0.771920756626.issue11344@psf.upfronthosting.co.za>
In-reply-to
Content
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)
/ggparent/gparent/parent

>>> dirname(path, 2)
/ggparent/gparent

>>> dirname(path, 3)
/ggparent

Perhaps we should throw ValueErrors for invalid height values:

>>> dirname(path, 10)
ValueError

>>> dirname(path, -1)
ValueError

Perhaps a height of 0 should do nothing:

>>> dirname(path, 0)
/ggparent/gparent/parent/myfile.txt

I can supply patches, unit tests and docs if you like.
History
Date User Action Args
2011-02-27 08:26:48blokeleysetrecipients: + blokeley
2011-02-27 08:26:48blokeleysetmessageid: <1298795208.87.0.771920756626.issue11344@psf.upfronthosting.co.za>
2011-02-27 08:26:48blokeleylinkissue11344 messages
2011-02-27 08:26:48blokeleycreate