tags:

views:

101

answers:

3

I'm using the CSV module to read a tab delimited file. Code below:

z = csv.reader(open('/home/rv/ncbi-blast-2.2.23+/db/output.blast'), delimiter='\t')

But when I add Z.close() to end of my script i get and error stating "csv.reader' object has no attribute 'close'"

z.close()

So how do i close "Z"?

+4  A: 

You do not close CSV readers directly; instead you should close whatever file-like object is being used. For example, in your case, you'd say:

f = open('/home/rv/ncbi-blast-2.2.23+/db/output.blast')
z = csv.reader(f, delimiter='\t')
...
f.close()

If you are using a recent version of Python, you can use the with statement, e.g.

with open('/home/rv/ncbi-blast-2.2.23+/db/output.blast') as f:
    z = csv.reader(f, delimiter='\t')
    ...

This has the advantage that f will be closed even if you throw an exception or otherwise return inside the with-block, whereas such a case would lead to the file remaining open in the previous example. In other words, it's basically equivalent to a try/finally block, e.g.

f = open('/home/rv/ncbi-blast-2.2.23+/db/output.blast')
try:
    z = csv.reader(f, delimiter='\t')
    ...
finally:
    f.close()
Eli Courtwright
+8  A: 

The reader is really just a parser. When you ask it for a line of data, it delegates the reading action to the underlying file object and just converts the result into a set of fields. So there's no need to close the reader; it'd be a meaningless operation.

You should make sure to close the underlying file object, though, and here's how:

with open('/home/rv/ncbi-blast-2.2.23+/db/output.blast') as f:
    z = csv.reader(f, delimiter='\t')

(If you're not familiar with the with statement, it basically encloses its contents in a try...finally block that closes the file in the finally part.)

David Zaslavsky
`csv.reader(f, delimiter='\t')` <- you missed the `f`
Thanatos
`with` was added in 2.5, so be careful if you're concerned about backwards compatibility.
Jared Forsyth
@Thanatos: yep, you must have commented while I was fixing that. And @Jared: good point about `with`, although given that Python 2.7 is the current stable version on the 2.X series, the versions prior to 2.5 are getting old.
David Zaslavsky
A: 

You don't close the result of the reader() method, you close the result of the open() method. So, use two statements: foo=open(...); bar=csv.reader(foo). Then you can call foo.close().

There are no bonus points awarded for doing in one line that which can be more readable and functional in two.

Bryan Oakley