Editor

Python Program to Read a File Line by Line

Beginner · File handling

Reading a file line by line means handling one line at a time instead of loading the whole file at once, so even a very large file can be processed. This program writes a small text file, then reads it back one line at a time and prints each line with its number.

Program

read_file_line_by_line.py
# Make a sample file to work on
with open("notes.txt", "w") as f:
f.write("Python is easy to read\n")
f.write("Files keep data after the program ends\n")
f.write("Practice a little every day\n")
with open("notes.txt", "r") as f:
line_number = 1
for line in f:
line = line.rstrip() # remove the newline at the end
print(f"Line {line_number}: {line}")
line_number += 1
Output
Line 1: Python is easy to read
Line 2: Files keep data after the program ends
Line 3: Practice a little every day

How it works

  1. The program first writes three lines to notes.txt, each ending in \n, so it has a file to read.
  2. with open("notes.txt", "r") as f opens the file for reading and closes it automatically when the block ends.
  3. for line in f gives one line of the file on each pass, without loading the whole file into memory.
  4. Every line read from a file still ends with its newline, so line.rstrip() removes it; otherwise print() would leave a blank line after each one.
  5. line_number starts at 1 and goes up by one each time, so every line is printed with its number.

Other ways to write it

Using readline() in a while loop

Using readline() in a while loop
# Make a sample file to work on
with open("notes.txt", "w") as f:
f.write("Python is easy to read\n")
f.write("Files keep data after the program ends\n")
f.write("Practice a little every day\n")
with open("notes.txt", "r") as f:
line = f.readline()
while line != "": # readline() returns "" at the end of the file
print(line.rstrip())
line = f.readline()
Output
Python is easy to read
Files keep data after the program ends
Practice a little every day

readline() reads one more line each time it is called and returns an empty string at the end of the file. A blank line inside the file comes back as "\n", not "", so it does not end the loop early.

Using readlines() and enumerate()

Using readlines() and enumerate()
# Make a sample file to work on
with open("notes.txt", "w") as f:
f.write("Python is easy to read\nFiles keep data after the program ends\nPractice a little every day\n")
with open("notes.txt", "r") as f:
lines = f.readlines() # a list with one string per line
print("The file has", len(lines), "lines")
for number, line in enumerate(lines, start=1):
print(number, line.rstrip())
Output
The file has 3 lines
1 Python is easy to read
2 Files keep data after the program ends
3 Practice a little every day

readlines() loads every line into a list at once, which is handy when you need the count or want to go back to a line, but for line in f uses less memory on big files.

Try it yourself

Add a blank line to the sample file and change the loop so it skips empty lines instead of printing them.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises