[Python-ideas] os.path.commonprefix: Yes that old chestnut.

Paul Moore p.f.moore at gmail.com
Mon Mar 23 23:56:17 CET 2015


On 23 March 2015 at 22:48, Paul Moore <p.f.moore at gmail.com> wrote:
> I'll see what I can do. Basically it's just

More specifically, as a pathlib.Path method:

def commonprefix(self, *rest):
    cp = []
    for p0, *ps in zip(self.parts, *[pp.parts for pp in rest]):
        if any(p0 != pi for pi in ps):
            break
        cp.append(p0)
    if not cp:
        return None
    return type(self)(*cp)

Paul


More information about the Python-ideas mailing list