Monday, October 8, 2012

Do you need to .close() reader objects? - it's just a parser, NO.


http://stackoverflow.com/questions/3216954/python-no-csv-close

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...finallyblock that closes the file in the finally part.)






2
http://stackoverflow.com/questions/12118609/python-str-object-has-no-attribute-close

file_content is a string variable, which contains contents of the file -- it has no relation to the file. The file descriptor you open with open(from_file) will be closed automatically: file sessions are closed after the file-objects exit the scope (in this case, immediately after .read()).



file_content = open(from_file).read()
new_file = open(to_file, 'w').write(file_content)

new_file.close()

open(...) returns a reference to a file object, calling read on that reads the file returning a string object, calling write writes to it returning None
neither of which have a close attribute.




3
http://stackoverflow.com/questions/1834556/does-a-file-object-automatically-close-when-its-reference-count-hits-zero


foo = open('foo').read()
would get you the file's contents and immediately close the file. However, after reading the answer tohttp://stackoverflow.com/questions/1832528/is-close-necessary-when-using-iterator-on-a-python-file-object I get the impression that this does not happen, and that calling .close() on a file object isalways necessary.



The answer is in the link you provided.
Garbage collector will close file when it destroys file object, but:
  • you don't really have control over when it happens.
    While CPython uses reference counting to deterministically release resources (so you can predict when object will be destroyed) other versions don't have to. For example both Jython or IronPython use JVM and .NET garbage collector which release (and finalize) objects only when there is need to recover memory and might not do that for some object until the end of the program. And even for CPython GC algorithm may change in the future as reference counting isn't very efficient.
  • if exception is thrown when closing file on file object destruction, you can't really do anything about it because you won't know.


But in any managed environment relying on garbage collector (tuned to free just memory) to clean up other resources is wrong direction. Of course if it's just short script and you simply read file (like in your example) it's OK to let GC or even OS close the file. – Tomek Szpakowicz Dec 2 '09 at 18:37






No comments:

Post a Comment