NEWBIE TIP: Reading a file two different ways...from ENGSOL
aleaxit at yahoo.com
Sat Aug 4 09:55:28 CEST 2001
<engsol at teleport.com> wrote in message
news:3b6b720d.12189167 at news.onemain.com...
> If it's in error, please post a correction.
> def readfile(foo_name): # Pretty much as above
> foo_file = open(foo_name, 'r')
> while 1:
> foo_line = foo_file.readline()
> if foo_line: # Notice we change the
> print foo_line, # the IF, just because
It should work fine, but it's needlessly complicated. Why not:
foo_file = open(foo_name, 'r')
for foo_line in foo_file.xreadlines():
if one wants to be Finalizationly Correct, or just:
for foo_line in open(foo_name, 'r').xreadlines():
if the potential risk of having a file stay open (for reading) a
little longer than strictly necessary (in implementations that
can't guarantee finalization, such as Jython) is not an issue.
More information about the Python-list