Using a for loop to read text files

 In addition to using the readline() function, we can also use a for loop. It is a more elegant and efficient way to read text files. The program below shows how it is done.


f = open('woohoo.txt', 'r')


for line in f:

    print(line, end = '')


f.close()


The for loop loops through the text file line by line.

Output

in order to guys,

you need to fall

but what if, in order to fall

you need to guys

No comments:

Post a Comment