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

Serhiy Storchaka storchaka at gmail.com
Wed Nov 7 09:51:46 CET 2012

On 07.11.12 09:22, Ronald Oussoren wrote:
>> It would also be a bit surprising that there are cases where 
>> commonpath(a,a) != a.
> That's already true, commonpath('/usr//bin', '/usr//bin') would be 
>   '/usr/bin' and not '/usr//bin'.

Yes, the current implementation does not preserve the repeated slashes, this is an argument for the answer that commonpath(['/usr//bin', '/usr/bin']) should return '/usr/bin' and not '/usr'.

However it would be a bit surprising that there are cases where commonpath([normpath(a), normpath(a)]) != normpath(a).

> Stripping '.' elements would be fine, e.g. commonpath('/usr/./bin/ls', 
> '/usr/bin/sh') could be '/usr/bin'.

May be.

> An empty string is not a valid path.  Now that I reconsider this 
> question: "." would be a valid path, and would have a sane meaning.

Looks reasonable, but I am not sure.  A returned value most probably will be used in join() and this will add an unexpected './' at the start of path.

More information about the Python-ideas mailing list