Pythonic way of reading a textfile line by line without throwing anexception

D

Deivys Ramirez

Hi everyone,

I'm a Python newbie but have experience programming PHP and C so i'm
not really new to programming but new to Python.

What's the "pythonic" way of reading a text file, line by line,
without throwing an exception when there's nothing to be read?

I've been trying to map my while(!eof(fp)) mindset to the file object
Python gives me when I call open() but have had no good results.

Regards,

Deivys Ramirez
Lima-Peru
 
L

Larry Bates

Deivys said:
Hi everyone,

I'm a Python newbie but have experience programming PHP and C so i'm
not really new to programming but new to Python.

What's the "pythonic" way of reading a text file, line by line,
without throwing an exception when there's nothing to be read?

I've been trying to map my while(!eof(fp)) mindset to the file object
Python gives me when I call open() but have had no good results.

Regards,

Deivys Ramirez
Lima-Peru

File objects are iterable so you you can just loop on them.

fp=open(filename, 'r')
for line in fp:
# do something with line

fp.close()

-Larry Bates
 
M

Matimus

fp=open(filename, 'r')
for line in fp:
# do something with line

fp.close()

Or, if you are using Python 2.5+, you can use the file context via the
`with' statement.

Code:
from __future__ import with_statement # this line won't be needed in
Python 2.6+

with open(filename, 'r') as fp:
    for line in fp:
        # do something with line

This basicly just closes the file automatically (even if there is an
exception) when the context (part indented under `with') is exited.
This is a new feature, but probably the most pythonic way of doing it.
That will be especially true in the future.

Matt
 
A

Asun Friere

What's the "pythonic" way of reading a text file, line by line,
without throwing an exception when there's nothing to be read?

I've been trying to map my while(!eof(fp)) mindset to the file object
Python gives me when I call open() but have had no good results.

As the previous posts have indicated the idiomatic (and highly
readable) way to read a file is simply to iterate across it. Just in
case you are ever in the position of having to use .readline()
directly, you might want to note that this method returns empty lines
as '\n' (go figure) and the EOF simply as ''. So you test of EOF
using 'if not line :' rather than anything like 'if eof :'
Historically, ie. before file objects were iterables, you would have
written something like this:

fobj = open('foo.txt')
while 1 :
line = fobj.readline()
if not line :
break
#do stuff to line
fobj.close()

Obviously iterating 'for line in fobj : ' is neater.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top