Newbie question regarding .readline()

David Goodger dgoodger at bigfoot.com
Sun Jul 30 01:16:20 EDT 2000


on 2000-07-30 00:33, jim roush (jar at mminternet.com) wrote:
> the problem is, line is only one character instead of an entire line.
> I used those print statements to verify this.  It's as if I used
> .read() instead.  If I use .readlines() instead, it reads multiple
> lines like it should.
> 
> What gives?

When you use .readline(), it returns a single string, which can be
interpreted as a sequence of 1-character strings. So "for line in
PK_INFILE.readline()" gives you one character at a time from a single line
(the first line only). Use "for line in PK_INFILE.readlines()" instead.
Doing "for line in PK_INFILE.read()" would loop one character at a time, as
in .readline(), except that it wouldn't stop at the end of the first line.

Try putting your print statements outside the loop, with each of .read(),
.readline(), and .readlines(), to see how they differ. ie:

    lines = PK_INFILE.readline()
    print lines
    for line in lines:
        etc.

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list