def _count_lines(filename: str):
    """
    Returns number of lines in a file in an optimized way. Useful for big files

    :param filename: Path to file
    :return: Number of lines
    :raise IOError: If the file couldn't be opened
    """
    lines = 0
    buf_size = 1024 * 1024

    with open(filename, "rb") as f:
        read_content = f.read(buf_size)
        while read_content:
            lines += read_content.count(b"\n")
            read_content = f.read(buf_size)

    return lines