[Tutor] Why different behaviours?

alan.gauld@bt.com alan.gauld@bt.com
Sat, 27 Oct 2001 17:01:17 +0100

> import string, sys
> f=open('d:/Dokument/Programprojekt/pythonworks/projects/H2/Lex
> icon.txt', 'r')
> for line in f.readline():

readline returns a string, line will therefore take the value 
of each char in the string.

> Also, what would be the way to read line after line until end 
> of file. How to detect end of file when reading a sequential 
> file this way?

If its a short-medium file use:

for line in f.readlines():  # note plural!

or for long files:

for line in f.xreadlines():  # x stops it all being read at once


while 1:
      line = f.readline()
      print line

There are other variations. For more words explaining 
whats going on try my tutor at:


look under the files topic...

Alan G