Thursday, February 4, 2010

I Have Found A File Called Python On My Pc Does Python Automatically Close Destroyed File Objects?

Does Python automatically close destroyed file objects? - i have found a file called python on my pc

Many examples of Python programs, the search and the actual content of a file like this:
data = open ( 'file.txt'). read ()

If you expand the party, which looks like this:
f = open ( 'file.txt')
Data = f.read ()

This is bad because the F points to open a file, in the end. Both examples use the first, I think, the file objects should be closed when the reference count of 0 results. IU "F" as "f.close ()".

However, I found no documentation on this issue. I wish I had solid evidence before leaving my old DOS habit close all open files.

1 comments:

om said...

The Python language does not require objects to be accessed in a particular manner, "the file. The application is free to do whatever he wants. This could also close the file as a side effect of garbage collection of the" Record "in question - or perhaps not. Although garbage collection is optional.

So your intuition is correct, you must explicitly close () all files you open ().

Post a Comment