'Is there a better way to add headers to CSV file using Python?

I am trying to add headers to existing csv files using a python script. I found a way to do it but it uses too much of my memory and causes my laptop to crash. I wanted to know if there's a better way to do it. I will post the code below.

#!/usr/bin/env python3

import csv

with open('MBAc.csv',newline='') as f:
    r = csv.reader(f)
    data = [line for line in r]
with open('MBAc.csv','w',newline='') as f:
    w = csv.writer(f)
    w.writerow(['student_number', 'State_Studentnumber', 'Attendance_codeid', 'Att_code', 'att_date', 'att_mode_code', 'SchoolID', 'YearID','Att_Comment'])
    w.writerows(data)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source