Finding the longest common prefix of an array of strings in Python
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*