2

Hi I have been experimenting with Winston Ewert's code example from this thread.

Python: Removing duplicate CSV entries

But I can't close my input and output files. What am I doing wrong?

write_outfile.close()

write_infile.close()

Traceback (most recent call last): File "Duplicates_01.py", line 26, in write_outfile.close() AttributeError: '_csv.writer' object has no attribute 'close'

import csv

write_infile = csv.reader(open('File1.csv', 'r'))
write_outfile = csv.writer(open('File2.csv', 'w'))

#write_infile = open('File1.csv', 'r')
#f1 = csv.reader(write_infile)
#f1 = csv.reader(write_infile, delimiter=' ')

#write_outfile = open('File2.csv', 'w')
#f2 = csv.writer(write_outfile)
#f2 = csv.writer(write_outfile, delimiter=' ')

phone_numbers = set()

for row in write_infile:
    if row[1] not in phone_numbers:
        write_outfile.writerow(row)
#       f2.writerow(row)
        phone_numbers.add(row[1])

# write_outfile.close()
# write_infile.close()

File1.csv

user, phone, email
joe, 123, joe@x.com
mary, 456, mary@x.com
ed, 123, ed@x.com
piggy
  • 83
  • 1
  • 3
  • 11

1 Answers1

7

by doing:

csv.reader(open('File1.csv', 'r'))

you're passing an anonymous file handle to csv.reader objects, so you cannot control when the file will be closed (it's this handle which needs to be closed, not the csv.reader object)

The close method must apply to the file handle (csv reader/writer objects can work on lists, iterators, ..., they can't have a close method) so I would do:

fr = open('File1.csv', 'r')

and

csv.reader(fr)

then

fr.close()

or use a context manager:

with open('File1.csv', 'r') as fr:
    csv.reader(fr)

and file will be closed as soon as you leave the context

Aside: there's an extra catch when creating csv files on some python versions. using a handle like open('File2.csv', 'w') may cause problems (blank lines inserted). For a compatible & robust way you can read this Q&A

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219