Scanning a file
James Stroud
jstroud at mbi.ucla.edu
Fri Oct 28 13:53:06 EDT 2005
On Friday 28 October 2005 06:29, Björn Lindström wrote:
> "pinkfloydhomer at gmail.com" <pinkfloydhomer at gmail.com> writes:
> > f = open("filename", "rb")
> > s = f.read()
> > sub = "\x00\x00\x01\x00"
> > count = s.count(sub)
> > print count
>
> That's a lot of lines. This is a bit off topic, but I just can't stand
> unnecessary local variables.
>
> print file("filename", "rb").read().count("\x00\x00\x01\x00")
The "f" is not terribly unnecessary, because the part of the code you didn't
see was
f.close()
Which would be considered good practice.
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/
More information about the Python-list
mailing list