简体   繁体   中英

Python: No csv.close()?

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"?

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. In Python 2.5+, here's the way to do that:

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. For Python 2.5 you'll need a __future__ import to enable the with statement. If you need to retain compatibility with earlier versions of Python like 2.4, you should do the closing yourself using try...finally .


Thanks to Jared for pointing out compatibility issues with the with statement.

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, eg

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, eg

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

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) 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM