Python - File Text File


To scan a text file line by line, file iterators are often your best option:


myfile = open('myfile.txt', 'w')        # Open for text output: create/empty 
myfile.write('hello text file\n')       # Write a line of text: string 
myfile.write('goodbye text file\n') 
myfile.close()                          # Flush output buffers to disk 
# w  w  w. j  a  va 2s. c o  m
for line in open('myfile.txt'):        # Use file iterators, not reads 
     print(line, end='')


Related Topics