all Python 3deprecated in 3.15osfunction
os.path.commonprefix
os.path — path parsing
Return the longest common prefix of a list of strings — character-wise, not path-aware.
Syntax
os.path.commonprefix(list)
It compares characters, not components, so for paths it gives a wrong answer: commonprefix(['/usr/lib', '/usr/local']) returns '/usr/l' — no such directory. Use commonpath for paths. The function itself is fine for any strings, e.g. finding the shared start of a word list. Since Python 3.15 the function is deprecated and emits a DeprecationWarning: for paths use commonpath(), for the shared start of strings compare characters, e.g. with zip and itertools.takewhile.
Examples
import os
print(os.path.commonprefix(['abcd', 'abef'])) # → ab
print(os.path.commonprefix(['flower', 'flow', 'flight'])) # → fl
print(os.path.commonprefix(['/usr/lib', '/usr/lib64'])) # → /usr/lib
print(os.path.commonpath(['/usr/lib', '/usr/lib64']).endswith('usr')) # → True
print(repr(os.path.commonprefix([]))) # → ''
See also
Open in the full glossarydocs.python.orgReport an inaccuracy