Terry Green wrote:
Am running this Script and cannot figure out how to close my files,

Keep getting msg: Attribute Error: '_csv.writer' object has no attribute
'close'

Why?

Lesson one: we're not mind readers. To be able to give you useful advise, we need to see the ACTUAL error and not a summary taken out of context. Otherwise we're left with making sarcastic replies like "because it doesn't have an attribute called 'close'", which makes us feel good but doesn't help you.

Python prints a traceback showing the exact error, including the full traceback of where it happens. Please COPY AND PASTE the FULL traceback. Do NOT summarize it, re-type it from memory, describe it in your own words, or otherwise change it in any way. The only exception is if the traceback is *huge*, you should say so, and only show the last few entries. If we need to see the rest, we'll tell you.

Lesson two: we rarely need to, and never want to, see your entire script. Best practice is for you to simplify the problem to the smallest piece of code that still fails in the same way. This has three advantages:

(1) By working through the problem yourself, 7 out of 10 times you'll work out what the problem is yourself. You will have learned a valuable problem-solving skill.

(2) By making the problem small, rather than big, you improve the chances that others will have the time and motivation to solve it for you.

(3) You make our life easier, which means we will be kindly disposed to you and be more likely to help.


In this case, I think I can solve the problem very easily, using the interactive interpreter:

>>> import csv
>>> help(csv.writer.close)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'close'


csv.writer doesn't have a close method, just because that's how it is. There's nothing to close. What you need to close is the file object you pass to the csv.writer.

import csv

fp = open('c:/users/terry/downloads/tup1012k/tup1012.csv', 'w')
output = csv.writer(fp)
# do stuff with output
# ...
# then close the underlying file object, not the writer.
fp.close()


--
Steven

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to