[Python-ideas] os.path.commonprefix: Yes that old chestnut.

Paul Moore p.f.moore at gmail.com
Tue Mar 24 14:45:37 CET 2015


On 24 March 2015 at 13:41,  <random832 at fastmail.us> wrote:
> On Tue, Mar 24, 2015, at 07:56, Paul Moore wrote:
>> For example, common_prefix('a/b/file.c', 'a/b/file.c'). The common
>> prefix is obviously 'a/b/file.c' - but I can imagine people *actually*
>> wanting the common *directory* containing both files. But taken
>> literally, that's only possible if you check the filesystem, so it
>> would no longer be a PurePath operation.
>
> Or you could _always_ reject the last component. That is, even if
> "file.c" is a directory, return the directory it is in rather than
> itself.

I'm pretty sure there would be cases where that's not right. For
example, commonprefix('a/b', 'a/b/file.c') would be 'a'. That seems
wrong.

> Does parts differentiate between "a/b/c" and "a/b/c/"? There are
> certainly contexts where real filesystems differentiate between them
> (symlinks, for example).

No, it doesn't:

>>> pathlib.Path('a/b/').parts
('a', 'b')
>>> pathlib.Path('a/b').parts
('a', 'b')

Paul


More information about the Python-ideas mailing list