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

Bruce Leban bruce at leapyear.org
Wed Nov 7 03:05:30 CET 2012


It would be nice if in conjunction with this os.path.commonprefix is
renamed as string.commonprefix with the os.path.commonprefix kept for
backwards compatibility (and deprecated).

more inline

On Tue, Nov 6, 2012 at 7:49 AM, Ronald Oussoren <ronaldoussoren at mac.com>wrote:

>
> On 6 Nov, 2012, at 16:27, Serhiy Storchaka <storchaka at gmail.com> wrote:
> > 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
>
> It appears that you want the result to never include a trailing /.
However, you've left out one key test case:

What is commonpath('/usr', '/var')?

It seems to me that the only reasonable value is '/'.

If you change the semantics so that it either (1) it always always includes
a trailing / or (2) it includes a trailing slash if the two paths have it
in common, then you don't have the weirdness that in this case it returns a
slash and in others it doesn't. I am slightly inclined to (1) at this point.

It would also be a bit surprising that there are cases where
commonpath(a,a) != a.



>  > 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
>

seems better than the alternative of interpreting the '..'.

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

Why? Why is an empty path not a reasonable result?


> * On windows two paths that don't have the same drive should raise an
> exception
>

I disagree. On unix systems, should two paths that don't have the same
drive also raise an exception? What if I'm using this function on windows
to compare two http paths or two paths to a remote unix system? Raising an
exception in either case would be wrong.


> 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.
>

Yes, don't return a useless value. An empty string is useful in the
relative path case and '/' is useful in the non-relative but paths don't
have common prefix at all case.


--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121106/29c7c97a/attachment.html>


More information about the Python-ideas mailing list