def make_path_relative(path, rel_to):
 path_filename = os.path.basename(path)
 path = os.path.dirname(path)
 path = os.path.normpath(os.path.abspath(path))
 rel_to = os.path.normpath(os.path.abspath(rel_to))
 path_parts = path.strip(os.path.sep).split(os.path.sep)
 rel_to_parts = rel_to.strip(os.path.sep).split(os.path.sep)
 while (path_parts and rel_to_parts and (path_parts[0] == rel_to_parts[0])):
  path_parts.pop(0)
  rel_to_parts.pop(0)
 full_parts = (((['..'] * len(rel_to_parts)) + path_parts) + [path_filename])
 if (full_parts == ['']):
  return ('.' + os.path.sep)
 return os.path.sep.join(full_parts)

def make_path_relative ( path , rel_to ) : path_filename = os . path . basename ( path ) path = os . path . dirname ( path ) path = os . path . normpath ( os . path . abspath ( path ) ) rel_to = os . path . normpath ( os . path . abspath ( rel_to ) ) path_parts = path . strip ( os . path . sep ) . split ( os . path . sep ) rel_to_parts = rel_to . strip ( os . path . sep ) . split ( os . path . sep ) while ( path_parts and rel_to_parts and ( path_parts [ 0 ] == rel_to_parts [ 0 ] ) ) : path_parts . pop ( 0 ) rel_to_parts . pop ( 0 ) full_parts = ( ( ( [ '..' ] * len ( rel_to_parts ) ) + path_parts ) + [ path_filename ] ) if ( full_parts == [ '' ] ) : return ( '.' + os . path . sep ) return os . path . sep . join ( full_parts )
make a filename relative .