def count_lines(file):
    """Given a file, returns the number of lines it contains.
    The current file position should be preserved as long as the file
    supports tell() and seek()."""
    old_position = file.tell()
    file.seek(0)
    count = 0
    while file.readline() != '':
        count += 1
    file.seek(old_position)
    return count