newbie question...
Skip Montanaro
skip at mojam.com
Tue Dec 28 16:37:31 EST 1999
abs> I'm wondering what the accepted method of handling a text file's
abs> contents are in Python. For example, the following Perl construct
abs> is pretty standard.
abs> while ( <SOMEFILEHANDLE> )
abs> {
abs> # process each line as if comes off the file handle...
abs> }
abs> is typical.
abs> The equivalent Python appears to be
abs> somefilehandle = open( "some/file/name.text" )
abs> all_the_lines_in_the_file = somefilehandle.readlines()
abs> somefilehandle.close()
abs> # now process the lines in the all_the_lines_... list
abs> # using some prefered method (there's more than
abs> # one way of doing this, of course ;-)
abs> THE BIG QUESTION: Am I understanding Python's philosophy properly?
Yeah, that's a pretty typical idiom for small files. For larger files
something like
somefilehandle = open( "some/file/name.text" )
line = somefilehandle.readline()
while line:
dofunstuff(line)
line = somefilehandle.readline()
somefilehandle.close()
Before you run off and compare that with the equivalent Perl and find it
lacking in performance, I'll interject that Python does nothing fancy with
its I/O. It's just built on top of stdio. For larger files you *can* chunk
the input using something like
sizehint = 10**6
somefilehandle = open( "some/file/name.text" )
lines = somefilehandle.readlines(sizehint)
while lines:
for line in lines:
dofunstuff(line)
lines = somefilehandle.readlines(sizehint)
somefilehandle.close()
It's not as succinct as the simpler idioms, but somewhat more efficient.
Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098 | Python: Programming the way Guido indented...
More information about the Python-list
mailing list