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 ypank
Recipients ypank
Date 2020-08-09.17:43:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1596994996.9.0.794186780316.issue41511@roundup.psfhosted.org>
In-reply-to
Content
As I can see, pathlib path parents don't support slicing with negative indexes: 

>>> import pathlib
>>> path = pathlib.PosixPath("some/very/long/path/here")
>>> path.parents[-1]
...
    raise IndexError(idx)
IndexError: -1

That's kinda weird for python. I mean, in regular list/etc if I need the last element, I'd normally do list[-1], but here to get the last parent, I need to actually know how many parents do I have. 

So now, I can do something like this:

>>> parents_count = len(path.parents) - 1
>>> path.parents[parents_count]
PosixPath('.')

So that's how I can get the last parent. But is it pythonic? No.

So, I decided to fix this, and now we can do negative slicing:

>>> path.parents[-1] == path.parents[parents_count]
True
>>> path.parents[-2] == path.parents[parents_count - 1]
True

So what do you guys think about this?
History
Date User Action Args
2020-08-09 17:43:16ypanksetrecipients: + ypank
2020-08-09 17:43:16ypanksetmessageid: <1596994996.9.0.794186780316.issue41511@roundup.psfhosted.org>
2020-08-09 17:43:16ypanklinkissue41511 messages
2020-08-09 17:43:16ypankcreate