Indent correctly; your
for statement should be inside the
import csv with open('v.csv', 'w') as csvfile: cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) for w, c in p.items(): cwriter.writerow(w + c)
with block, the file is closed.
>>> with open('/tmp/1', 'w') as f: ... print(f.closed) ... False >>> print(f.closed) True