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:
- how to check namespace for all open file objects
- 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]
 
    