Author eric.snow
Recipients Roman.Evstifeev, eric.araujo, eric.smith, eric.snow, ezio.melotti, loewis, r.david.murray, ronaldoussoren, santoso.wijaya
Date 2011-08-01.21:21:59
SpamBayes Score 0.0004812
Marked as misclassified No
Message-id <1312233719.86.0.899538092477.issue10395@psf.upfronthosting.co.za>
In-reply-to
Content
You can already get the better prefix using os.path, albeit less efficiently.  Here's an example:

def commondirname(paths):
    subpath = os.path.commonprefix(paths)
    for path in paths:
        if path == subpath:
            return subpath
    else:
        return os.path.join(os.path.split(subpath)[0], "")

However, would it be better to implicitly normalize paths first rather than doing a character-by-character comparison?  Here is an unoptimized demonstration of what I mean:

def commondirname(paths):
    result = ""
    for path in paths:
        path = os.path.normcase(os.path.abspath(path))
        if not result:
            result = path
        else:
            while not path.startswith(result + os.path.sep):
                result, _ = os.path.split(result)
                if os.path.splitdrive(result)[1] == os.path.sep:
                    return result
    return result
History
Date User Action Args
2011-08-01 21:22:00eric.snowsetrecipients: + eric.snow, loewis, ronaldoussoren, eric.smith, ezio.melotti, eric.araujo, r.david.murray, santoso.wijaya, Roman.Evstifeev
2011-08-01 21:21:59eric.snowsetmessageid: <1312233719.86.0.899538092477.issue10395@psf.upfronthosting.co.za>
2011-08-01 21:21:59eric.snowlinkissue10395 messages
2011-08-01 21:21:59eric.snowcreate