csv writer not closing file

后端 未结 5 1177
小蘑菇
小蘑菇 2020-12-05 23:36

im reading a csv file and then writing a new one:

import csv

with open(\'thefile.csv\', \'rb\') as f:
  data = list(csv.reader(f))

import collections
count         


        
5条回答
  •  执笔经年
    2020-12-06 00:39

    You can break out the open command into its own variable, so that you can close it later.

    f = open('/pythonwork/thefile_subset1.csv', 'w')
    writer = csv.writer(f)
    f.close()
    

    csv.writer throws a ValueError if you try to write to a closed file.

提交回复
热议问题