Message129616
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. |
|
Date |
User |
Action |
Args |
2011-02-27 08:26:48 | blokeley | set | recipients:
+ blokeley |
2011-02-27 08:26:48 | blokeley | set | messageid: <1298795208.87.0.771920756626.issue11344@psf.upfronthosting.co.za> |
2011-02-27 08:26:48 | blokeley | link | issue11344 messages |
2011-02-27 08:26:48 | blokeley | create | |
|