Issue 28560: Implement PurePath.startswith and PurePath.endswith (original) (raw)

Created on 2016-10-29 21:59 by cool-RR, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg279699 - (view) Author: Ram Rachum (cool-RR) * Date: 2016-10-29 21:59
Today I had to check whether a path object started with `/foo`. The nicest way I could think of was `str(p).startswith('/foo')`. What do you think about implementing `p.startswith('/foo')`?
msg279713 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2016-10-30 08:32
Do you mean that /foo was just an arbitrary string, rather than one of the parent directories? Because in that case it seems a perfectly correct way to go about comparison. If /foo was one of the parents, there are plenty of options: .parts, .parents, .relative_to. It seems like a fairly rare usecase to extend the api.
msg279716 - (view) Author: Ram Rachum (cool-RR) * Date: 2016-10-30 09:48
I mean that `/foo` is one of the parent directories. "there are plenty of options ..." Can you please spell them out precisely? The full line of code.
msg279719 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-30 10:03
>>> from pathlib import Path >>> p = Path('/foo/bar') >>> Path('/foo') in p.parents True >>> Path('/spam') in p.parents False >>> p.parts[:2] == ('/', 'foo') True >>> p.parts[:2] == ('/', 'spam') False >>> p.relative_to('/foo') PosixPath('bar') >>> p.relative_to('/spam') Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.5/pathlib.py", line 864, in relative_to .format(str(self), str(formatted))) ValueError: '/foo/bar' does not start with '/spam'
msg279721 - (view) Author: Ram Rachum (cool-RR) * Date: 2016-10-30 11:16
Thanks.
History
Date User Action Args
2022-04-11 14:58:38 admin set github: 72746
2016-10-30 11:44:46 SilentGhost set resolution: wont fix -> rejectedstage: resolved
2016-10-30 11:16:46 cool-RR set status: open -> closedresolution: wont fixmessages: +
2016-10-30 10:03:25 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2016-10-30 09:48:36 cool-RR set messages: +
2016-10-30 08:32:36 SilentGhost set nosy: + SilentGhostmessages: +
2016-10-29 21:59:33 cool-RR create