Semantics of file.close()

Dan Bishop danb_83 at yahoo.com
Mon Jul 16 20:04:08 EDT 2007


On Jul 16, 6:35 pm, seerdec... at gmail.com wrote:
> Hello,
>
> I'm a Python beginner and I'm trying to open, write and close a file
> in a
> correct manner. I've RTFM, RTFS, and I've read this thread:http://groups.google.ca/group/comp.lang.python/browse_thread/thread/7...
>
> I still cannot figure out the semantic of file.close(). As far as I
> can
> tell it is undocumented.

It's documented.  Not necessarily very well, but it's documented.
Type help(file.close) at the interactive prompt.


close(...)
    close() -> None or (perhaps) an integer.  Close the file.

    Sets data attribute .closed to True.  A closed file cannot be used
for
    further I/O operations.  close() may be called more than once
without
    error.  Some kinds of file objects (for example, opened by
popen())
    may return an exit status upon closing.

> How do I ensure that the close() methods in my finally clause do not
> throw an exception?

def quiet_close(fp):
    """Close a file, silently ignoring errors."""
    try:
       fp.close()
    except IOError:
       pass




More information about the Python-list mailing list