Pipeline Tasks, write to CSV

Screen Link:

My Code:

import csv

log = open('example_log.txt')
parsed = parse_log(log)
def build_csv(lines, file, header=None):
    if header:
        lines = [header] + [l for l in lines]
    writer = csv.writer(file, delimiter=',')
    writer.writerows(lines)
    file.seek(0)
    return file

file = open('temporary.csv', 'r+')
csv_file = build_csv(
    parsed,
    file,
    header=[
        'ip', 'time_local', 'request_type',
        'request_path', 'status', 'bytes_sent',
        'http_referrer', 'http_user_agent'
    ]
)
    
contents = csv_file.readlines()
print(contents[:5])

What I expected to happen:
I had copy-pasted the given answer as it was throwing errors for me to see the correct answer.

What actually happened:

but the given answer for this task also threw an error

contents is longer than we expected.

The variable contents is a list and it says is longer than expected, the given answer for this task too is not working, please provide the correct solution for this task.

I encounter the same issue but at the next step, when using itertools module to chain [header] and lines.

At the previous step with lines = [header] + [l for l in lines] it worked, but it raises the following error contents is longer than we expected while using itertools.