Given a list of pathnames, returns the longest common leading component
(m)
| 103 | |
| 104 | # Return the longest prefix of all list elements. |
| 105 | def commonprefix(m): |
| 106 | "Given a list of pathnames, returns the longest common leading component" |
| 107 | if not m: return '' |
| 108 | # Some people pass in a list of pathname parts to operate in an OS-agnostic |
| 109 | # fashion; don't try to translate in that case as that's an abuse of the |
| 110 | # API and they are already doing what they need to be OS-agnostic and so |
| 111 | # they most likely won't be using an os.PathLike object in the sublists. |
| 112 | if not isinstance(m[0], (list, tuple)): |
| 113 | m = tuple(map(os.fspath, m)) |
| 114 | s1 = min(m) |
| 115 | s2 = max(m) |
| 116 | for i, c in enumerate(s1): |
| 117 | if c != s2[i]: |
| 118 | return s1[:i] |
| 119 | return s1 |
| 120 | |
| 121 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 122 | # describing the same file? |