Three ways to read the contents of the file line by line python

method one:

Open = f ( " foo.txt " )              # returns a file object 
Line = f.readline ()              # readline call file () method 
the while Line: 
     Print Line,                  # followed by ',' ignores newline 
    # Print (Line , end = '') #. 3 used in the Python 
    Line = f.readline ()

f.close() 

Method Two:

for line in open("foo.txt"): 
    print line,

Method three:

f = open("c:\\1.txt","r") 
Lines = f.readlines () # read the entire contents 
for Line in Lines 
     Print Line

Guess you like

Origin www.cnblogs.com/yoyowin/p/12168051.html