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

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


On 24 March 2015 at 13:36,  <random832 at fastmail.us> wrote:
> On Tue, Mar 24, 2015, at 06:05, Paul Moore wrote:
>> One thing my implementation doesn't (yet) handle is case sensitivity.
>> The common prefix of WindowsPath('c:\\FOO\\bar') and
>> WindowsPath('C:\\Foo\\BAR') should be WindowsPath('C:\\Foo'). But not
>> for PosixPath. (And again, when they are mixed, which is silly but
>> possible, what behaviour should apply? "Work like self" is the obvious
>> answer if we have a method).
>
> So, speaking of windows path oddities... what should be done for paths
> where one has a drive and the other does not? No common prefix?
> Interpret as the current drive? Do the former for pure paths and the
> latter for concrete paths? Pass in an option?

It's worth pointing out that all of these edge cases only occur for
relative paths. It's quite possible that the only actual use cases
would be completely fine if the operation was only defined on absolute
paths.

My instinct says you do this *purely* based on the common prefix of
path.parts. The behaviour is then easy to define, and if it's not
precisely what someone wants they can convert the paths to absolute
and do the operations with that. I'd have to see real use cases to
justify anything else.

> Keeping in mind that people may write "portable" code but put in no
> effort to specifically support windows.

Precisely :-)

>> And again, when they are mixed, which is silly but possible, what behaviour should apply
>
> For concrete paths, using the actual names of the files if they exist
> would be another option.

You can't have mixed concrete paths, you can only ever instantiate one
of PosixPath or WindowsPath on a given system, AIUI.
Paul


More information about the Python-ideas mailing list