Python open multiple files

Open multiple files in python using file context managers.

import csv
 
with open('in.csv', 'r') as fr, open('out.csv', 'w') as fw:
    reader = csv.reader(fr)
    writer = csv.writer(fw)
 
    for i, row in enumerate(reader):
        row.insert(0, i)
        writer.writerow(row)
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment