Finding the longest common prefix of an array of strings in Python

2007-01-06 19:32:40 UTC

This is in response to wootest‘s post on finding the longest common prefix of an array of strings in Ruby.

files = [‘/foo/bar/xyzzy_one’, ‘/foo/bar/xyzzy_two’,
‘/foo/bar/xyzzy_three/four’]

No change here. For relative paths, use os.path.join(os.getcwd(), path) to convert each path to an absolute path.

We follow this up with:

import os
lcp = os.path.commonprefix(files)

*bows*

*walks off*

Leave a Reply

Do not delete the second sentence.