[Python-ideas] PEP 428 - object-oriented filesystem paths
Steven D'Aprano
steve at pearwood.info
Sun Oct 7 03:41:44 CEST 2012
On 07/10/12 04:08, Antoine Pitrou wrote:
> Personally, I cringe everytime I have to type
> `os.path.dirname(os.path.dirname(os.path.dirname(...)))` to go two
> directories upwards of a given path. Compare, with, say:
I would cringe too if I did that, because it goes THREE directories
up, not two:
py> path = '/a/b/c/d'
py> os.path.dirname(os.path.dirname(os.path.dirname(path)))
'/a'
:)
>>>> p = Path('/a/b/c/d')
>>>> p.parent(2)
> PosixPath('/a/b')
You know, I don't think I've ever needed to call dirname more than
once at a time, but if I was using it a lot:
parent = os.path.dirname
parent(parent(parent(p))
which is not as short as p.parent(3), but it's still pretty clear.
--
Steven
More information about the Python-ideas
mailing list