We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.



Close the file. A closed file cannot be read or written any more. Any operation which requires that the file be open will raise a ValueError after the file has been closed. Calling file.close more than once is allowed.

As of Python 2.5, you can avoid having to call this method explicitly if you use the with statement. For example, the following code will automatically close f when the with block is exited:

from __future__ import with_statement

with open("hello.txt") as f:
    for line in f:
        print line

In older versions of Python, you would have needed to do this to get the same effect:

f = open("hello.txt")
    for line in f:
        print line

Note: Not all “file-like” types in Python support use as a context manager for the with statement. If your code is intended to work with any file-like object, you can use the closing() function in the contextlib module instead of using the object directly. See section [25.5][18] for details.