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

M

Michele Simionato

I have just discovered that the syntax

with file(name, 'w') as f:
do_something(f)

does not close the file at the end of the with statement! On the
contrary

with open(name, 'w') as f:
do_something(f)

works fine. The docs say "When opening a file, it’s preferable to use
open() instead of invoking this constructor directly." but perhaps
they should mention why :(
 
P

Peter Otten

Michele said:
I have just discovered that the syntax

with file(name, 'w') as f:
do_something(f)

does not close the file at the end of the with statement! On the
contrary

with open(name, 'w') as f:
do_something(f)

works fine. The docs say "When opening a file, it’s preferable to use
open() instead of invoking this constructor directly." but perhaps
they should mention why :(

Are you sure?

Python 2.6.4 (r264:75706, Nov 2 2009, 14:44:17)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information..... print >> f, "whatever"
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

Peter
 
D

Diez B. Roggisch

Michele said:
I have just discovered that the syntax

with file(name, 'w') as f:
do_something(f)

does not close the file at the end of the with statement! On the
contrary

with open(name, 'w') as f:
do_something(f)

works fine. The docs say "When opening a file, it’s preferable to use
open() instead of invoking this constructor directly." but perhaps
they should mention why :(

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
 
M

Michele Simionato

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.
 
D

Diez B. Roggisch

Michele said:
Python 2.5, but it could be an artifact of the way I am looking if a
file is closed.

The above ran on python2.5 for me, no hitch.
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.

Probably. Closing a file through with might not involve calling close() - it
might be implemented differently in __exit__.

So overload *that*, too.

Diez
 
P

Peter Otten

Michele said:
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.

Subclassing file doesn't work properly:

http://mail.python.org/pipermail/python-list/2005-April/920562.html

Peter
 
M

Michele Simionato

In Python 2.5 you have to implement your own __enter__ and __exit__
methods if you subclass from file. The file.__exit__ function doesn't
call f.close().

Yes, that was my problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top