Post

PythonPython

r
rushi chowdary

Posted on 26th April 2024|139 views

0
votes

Python Read File Line By Line

How to read file line by line in python?

Answers
P
chris bons

Posted on 26th April 2024

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

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos