def dirname(p):
    """Returns the directory component of a pathname"""
    i = p.rfind('/') + 1
    assert i >= 0
    head = p[:i]
    if head and head != '/' * len(head):
        head = head.rstrip('/')
    return head