"file" does not work with the "with" statement!

Michele Simionato michele.simionato at gmail.com
Thu Dec 10 05:29:20 EST 2009


On Dec 10, 11:04 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> Are you sure? For me on python2.5, it works as advertised:
>
> from __future__ import with_statement
>
> def test(outf):
>     with outf:
>         outf.write("test\n")
>     try:
>         outf.write("test\n")
>         assert False, "Not closed"
>     except ValueError:
>         pass
>
> outf = open("/tmp/foo", "w")
> test(outf)
> outf = file("/tmp/bar", "w")
> test(outf)
>
> Which Python do you use?
>
> Diez

Python 2.5, but it could be an artifact of the way I am looking if a
file is closed.
I have subclassed the file builtin, added a .close method and it was
not called by the
"with" statement. This during debugging, but now I have found another
reason to explain why I was
running out of file descriptors, (I was opening too many connections
to the db).
It is entirely possible that the problem was not with the "with"
statement.



More information about the Python-list mailing list