Posted on 17th September 2024|144 views
How to read file line by line in python?
Posted on 17th September 2024| views
You can use readlines() method to read a file line by line here is the example
L = ["Mindmajix\n", "Is\n", "Best\n"]
filex = open('myfile.txt', 'w')
filex.writelines(L)
filex.close()
filex = open('myfile.txt', 'r')
Lines = filex.readlines()
count = 0
for line in Lines:
print("Line{}: {}".format(count, line.strip()))
Output:
Line1: Mindmajix
Line2: Is
Line3: Best