greenik
greenik

Reputation: 11

AttributeError: '_csv.writer' object has no attribute 'close'

my code:

# opening CSV
with open('notables.csv', 'w') as notables:
    file_write=csv.writer(notables, delimiter = ',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    # write
    # TO DO: how to get the values to write as well?
    file_write.writerow(data1)
    
    # close file
    file_write.close()

my error:

 file_write.close()

 AttributeError: '_csv.writer' object has no attribute 'close'

Am I suppose to close a csv file when done? If so, how?

Upvotes: 1

Views: 1309

Answers (1)

Zach Young
Zach Young

Reputation: 11188

As others pointed out, you're trying to close something that isn't close-able. Just remove that line, and because you opened the file in a with ... context, you don't need to close anything, Python will manage that for you:

# opening CSV
with open('notables.csv', 'w') as notables:
    file_write=csv.writer(notables, delimiter = ',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    # write
    # TO DO: how to get the values to write as well?
    file_write.writerow(data1)

Upvotes: 1

Related Questions