我们从Python开源项目中,提取了以下38个代码示例,用于说明如何使用posixpath.commonprefix()。
def relpath(path, start=curdir): """Return a relative version of a path""" if not path: raise ValueError("no path specified") if type(start) is unicode: start_list = unicode_abspath(start).split(sep) else: start_list = abspath(start).split(sep) if type(path) is unicode: path_list = unicode_abspath(path).split(sep) else: path_list = abspath(path).split(sep) # Work out how much of the filepath is shared by start and path. i = len(commonprefix([start_list, path_list])) rel_list = [pardir] * (len(start_list)-i) + path_list[i:] if not rel_list: return curdir return join(*rel_list) # End Futures
def is_suburi(self, base, test): """Check if test is below base in a URI tree Both args must be URIs in reduced form. """ if base == test: return True if base[0] != test[0]: return False common = posixpath.commonprefix((base[1], test[1])) if len(common) == len(base[1]): return True return False
def _relpath(self, path, start): """An alternative to os.path.relpath()""" if not path: raise ValueError("no path specified") start_list = posixpath.abspath(start).split(posixpath.sep) path_list = posixpath.abspath(path).split(posixpath.sep) # Work out how much of the filepath is shared by start and path. i = len(posixpath.commonprefix([start_list, path_list])) rel_list = [posixpath.pardir] * (len(start_list) - i) + path_list[i:] if not rel_list: return posixpath.curdir return posixpath.join(*rel_list)