Scanning a file

Alex Martelli aleaxit at
Sat Oct 29 23:14:59 CEST 2005

Tim Roberts <timr at> wrote:
> >> print file("filename", "rb").read().count("\x00\x00\x01\x00")
> >
> >Funny you should say that, because I can't stand unnecessary one-liners.
> >
> >In any case, you are assuming that Python will automagically close the
> >file when you are done.
> Nonsense.  This behavior is deterministic.  At the end of that line, the
> anonymous file object out of scope, the object is deleted, and the file is
> closed.

In today's implementations of Classic Python, yes.  In other equally
valid implementations of the language, such as Jython, IronPython, or,
for all we know, some future implementation of Classic, that may well
not be the case.  Many, quite reasonably, dislike relying on a specific
implementation's peculiarities, and prefer to write code that relies
only on what the _language_ specs guarantee.


More information about the Python-list mailing list