how to use readline in python 1

how to use readline in python

myfile = open("demo.txt", "r")
myline = myfile.readline()
while myline:
    print(myline)
    myline = myfile.readline()
myfile.close()   

Here is what the above code is Doing:
1. The first line opens the file in read mode.
2. The second line reads the first line of the file and assigns it to the myline variable.
3. The third line starts a while loop that runs as long as myline contains text.
4. The fourth line prints the value of myline.
5. The fifth line reads the next line of the file and assigns it to myline.
6. The sixth line closes the file.

Similar Posts