Python-Ref > Using files > Reading and writing files > Reading a file line by line, different approach
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Reading a file line by line, different approach

There is another approach to reading files line by line. I uses the readline method of a file object. The end of the file is reached when the function returns an empty string.
Expand/Shrink
This is an example text in an example file.
It even has
several
lines.
Zdroj: (fileio3-1.py)
  1   f = file( "example.txt", "r")  # we open the file "example.txt" for "r"eading
  2   line = f.readline()
  3   while line:
  4       print line
  5       line = f.readline()
  6   
  7   f.close()
stdout:
This is an example text in an example file.

It even has

several

lines.

Doba běhu: 21.2 ms