classification
Title: Implement `PurePath.startswith` and `PurePath.endswith`
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, cool-RR, pitrou, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-10-29 21:59 by cool-RR, last changed 2016-10-30 11:44 by SilentGhost. 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 "<stdin>", line 1, in <module>
  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
2016-10-30 11:44:46SilentGhostsetresolution: wont fix -> rejected
stage: resolved
2016-10-30 11:16:46cool-RRsetstatus: open -> closed
resolution: wont fix
messages: + msg279721
2016-10-30 10:03:25serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg279719
2016-10-30 09:48:36cool-RRsetmessages: + msg279716
2016-10-30 08:32:36SilentGhostsetnosy: + SilentGhost
messages: + msg279713
2016-10-29 21:59:33cool-RRcreate