[Python-Dev] os.path.commonprefix

Skip Montanaro skip@pobox.com
Sat, 7 Dec 2002 10:33:49 -0600


    Armin> I recently discovered that os.path.commonprefix(list-of-strings)
    Armin> returns the longest substring that is an initial segment of all
    Armin> the given strings, and that this has nothing to do with the fact
    Armin> that the strings might be paths.  

:-) I discovered the same thing over a year ago, fixed the problem in CVS,
then got roundly booed because I'd changed the semantics.  I think it broke
some bit of code of Mark Hammond's.  I recently used this function to find
the common prefix in a bunch of email addresses and prefaced it with this
comment:

  # don't be fooled by "os.path." - commonprefix operates char-by-char!
  pfx = os.path.commonprefix(all_addrs)

The Tools/scripts/trace.py file has a component-wise implementation of
commonprefix().

Skip