[Tutor] Unusual behavior in readline

Luke Paireepinart rabidpoobear at gmail.com
Tue Aug 8 20:51:13 CEST 2006


Tony Cappellini wrote:
>
> I don't understand why readline is producing such unusual behavior.
> I don't remember it working like this previously. The docs say it is 
> supposed to read a line at a time.
It does :)
>
> this function
>
> def ProcessFile(self, Inputfile, Outputfile=None):
>        
>         try:
>             fh=open(Inputfile,"r")
>         except IOError:
>             print"\nError ocurred opening %s for input\n" % Inputfile
>         else:
>             FixedEntry = []
>             Entry = ""
>             for Entry in fh.readline():
what you're saying in this line is:
1. call the readline method of fh.
2. iterate over each item in the returned value, and assign each item 
the name 'Entry' while in the loop.

What does the readline method of fh return?
it returns a single line.
So you're looping over a single line,
which means that each time through the loop Entry points to a single 
character.

What you want to do is call readline through each iteration of the loop.
or, to make it much easier, you could just use readlines :)
'for Entry in fh.readlines():'

> [snip stuff]
> Readline is supposed to read an entire line on each call, yet it is 
> only reading one character.
>
No, it is, in fact, reading an entire line on _each_ call, the only 
problem being that you only call it once.
HTH,
-Luke

>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list