-1

When running this simple script, the "output_file.csv" remains open. I am unsure about how to close the file in this scenario.

I have looked at other examples where the open() function is assigned to a variable such as 'f', and the object closed using f.close(). Because of the with / as csv-file, I am unclear as to where the file object actually is. Would anyone mind conceptually explaining where the disconnect is here? Ideally, would like to know:

  1. how to check namespace for all open file objects
  2. how to determine the proper method for closing these objects

simple script to read columns of data where mapping in column 1 is blank, fill down

import csv

output_file = csv.writer(open('output_file.csv', 'w'))
csv.register_dialect('mydialect', doublequote=False, quotechar="'")

def csv_writer(data):
    with open('output_file.csv',"ab") as csv_file:
        writer = csv.writer(csv_file, delimiter=',', lineterminator='\r\n', dialect='mydialect')
        writer.writerow(data)

D = [[]]
for line in open('inventory_skus.csv'):
    clean_line = line.strip()
    data_points = clean_line.split(',')
    print data_points
    D.append([line.strip().split(',')[0], line.strip().split(',')[1]])

D2 = D
for i in range(1, len(D)):
    nr = D[i]
    if D[i][0] == '':
        D2[i][0] = D[i-1][0]
    else:
        D2[i] = D[i]

for line in range(1, len(D2)):
    csv_writer(D2[line])
    print D2[line]
Jonathan
  • 781
  • 8
  • 20
  • You're doing an unnecessary explicit `open` at the top of the script, and you don't close it. So why are you surprised that the file remains opened? – BartoszKP Apr 29 '15 at 18:03
  • possible duplicate of [python csv - not closing file](http://stackoverflow.com/questions/3347775/python-csv-not-closing-file) – BartoszKP Apr 29 '15 at 18:04
  • I initially tried putting output_file.close() at the end but am getting this error: output_file.close() AttributeError: '_csv.writer' object has no attribute 'close' removing the initial output_file = csv.writer(open('output_file.csv', 'w')) still caused the file to remain open – Jonathan Apr 29 '15 at 18:07

1 Answers1

0

Actually, you are creating two file objects (in two different ways). First one:

output_file = csv.writer(open('output_file.csv', 'w'))

This is hidden within a csv.writer and not exposed by the same, however you don't use that output writer at all, including not closing it. So it remains open until garbage collected.

In

 with open('output_file.csv',"ab") as csv_file:

you get the file object in csv_file. The context block takes care of closing the object, so no need to close it manually (file objects are context managers).

Manually indexing over D2 is unnecessary. Also, why are you opening the CSV file in binary mode?

def write_data_row(csv_writer, data):
    writer.writerow(data)

with open('output_file.csv',"w") as csv_file:
    writer = csv.writer(csv_file, delimiter=',', lineterminator='\r\n',   dialect='mydialect')

    for line in D2[1:]:
        write_data_row(writer, line)
        print line
dhke
  • 15,008
  • 2
  • 39
  • 56
  • Ah, thank you. I am removing the unnecessary output_file at the beginning. However, the second object seems to not be closed by the context block. When I run in console, 'csv_file' is not defined outside of this block so am unsure how to close it. – Jonathan Apr 29 '15 at 18:17
  • How do you determine that it is not closed? If I try `writer.writerow('abc')` after the with-block I get `ValueError: I/O operation on closed file`. – dhke Apr 29 '15 at 19:40
  • when opening the output_file.csv i get an error from excel saying in use by another user/program – Jonathan Apr 29 '15 at 20:11
  • File objects have a `closed` property. Check that after the `with` block. Have you made sure that you do not have other references to the same file (like the one you just removed)? – dhke Apr 30 '15 at 10:11