Ray's playground

 

Computing the Relative Path from One Directory to Another(Python cookbook 2-22)

Problem

You need to know the relative path from one directory to anotherfor example, to create a symbolic link or a relative reference in a URL.

Solution

Precondition

itertools.izip(*iterables)

Make an iterator that aggregates elements from each of the iterables. Like zip()except that it returns an iterator instead of a list. Used for lock-step iteration over several iterables at a time.

itertools.izip

 

Code

all_equal: return True if all the elements are equal, otherwise False.

all_equal

 

common_prefix: return a list of common elements at the start of all sequences, then a list of lists that are the unique tails of each sequence.

common_prefix

 

relpath: return a relative path from p1 equivalent to path p2. In particular: the empty string, if p1 == p2; p2, if p1 and p2 have no common prefix.

relpath

 

 

posted on 2009-02-24 22:27  Ray Z  阅读(327)  评论(0编辑  收藏  举报

导航