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

Ronald Oussoren ronaldoussoren at mac.com
Tue Nov 6 16:49:42 CET 2012


On 6 Nov, 2012, at 16:27, Serhiy Storchaka <storchaka at gmail.com> wrote:

> See http://bugs.python.org/issue10395.
> 
> os.path.commonpath() should be a function which returns right longest common sub-path for specified paths (os.path.commonprefix() is completely useless for this).
> 
> There are some open questions about details of *right* behavior.
> 
> 
> 
> What should be a common prefix of '/var/log/apache2' and
> '/var//log/mysql'?

/var/log

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

/usr

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

/usr/local

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

/usr/local

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

/usr/bin

In all cases the path is first split into its elements, then calculate the largest common prefix of the two sets of elements, then join the elements back up again.

Some cases you don't mention:

* Relative paths that don't share a prefix should raise an exception
* 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.

> 
> Please, those who are interested in this feature, give consistent answers to these questions.

Ronald




More information about the Python-ideas mailing list