[Python-ideas] os.path.commonpath()

Serhiy Storchaka storchaka at gmail.com
Wed Nov 7 09:20:59 CET 2012

On 06.11.12 17:49, Ronald Oussoren wrote:
> On 6 Nov, 2012, at 16:27, Serhiy Storchaka <storchaka at gmail.com> wrote:
>> There are some open questions about details of *right* behavior.

I only asked the questions for which there are different opinions or for which I myself doubt.

>> What should be a common prefix of '/var/log/apache2' and
>> '/var//log/mysql'?
> /var/log

I think so too.

>> What should be a common prefix of '/usr' and '//usr'?
> /usr

normpath() preserves leading double slash (but not triple).  That's why I asked the question.

>> What should be a common prefix of '/usr/local/' and '/usr/local/'?
> /usr/local

os.path.split('/usr/local/') is ('/usr/local', '').  Repeated application of os.path.split() gives us ('/', 'usr', 'local', '').  That's why I assume that it is possible appropriate here to preserve the trailing slash.  I'm not sure.

>> What should be a common prefix of '/usr/local/' and '/usr/local/bin'?
> /usr/local

Here the same considerations as for the previous question.  In any case a common prefix of '/usr/local/etc' and '/usr/local/bin' should be '/usr/local'.

> * Relative paths that don't share a prefix should raise an exception

I disagree.  A common prefix for relative paths on the same drive is a current directory on this drive (if we decide to drop '..').

> * On windows two paths that don't have the same drive should raise an exception
> The alternative is to return some arbitrary value (like None) that you have to test for, which would IMHO make it too easy to accidently pass an useless value to some other API and get a confusing exeption later on.

May be.  This should be the same result (None or an exception) as for empty list or mixing of absolute and relative paths.

Thank you for your answers.

More information about the Python-ideas mailing list