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)
If you liked this posting, please feel free to leave me a comment below.
View this page on GitHub.
Posted .

Comments

Leave a Reply