Is it possible to use the with
statement directly with CSV files? It seems natural to be able to do something like this:
import csv
with csv.reader(open("myfile.csv")) as reader:
# do things with reader
But csv.reader doesn't provide the __enter__
and __exit__
methods, so this doesn't work. I can however do it in two steps:
import csv
with open("myfile.csv") as f:
reader = csv.reader(f)
# do things with reader
Is this second way the ideal way to do it? Why wouldn't they make csv.reader directly compatible with the with statement?