Reading Text Files

Python File Reading Explained — read(), readline(), readlines() for Beginners

Reading files is one of the most important skills in Python. Whether you are processing data, reading configs, or parsing logs — it all starts with knowing how to open and read a file correctly. In this post we will cover every method with real examples, outputs, and common traps to avoid.


📄 Our Sample File: test1.txt

All examples in this post use a file called test1.txt saved in the same folder as your Python script. It has exactly 3 lines:

Apple
Banana
Cherry

Create it by running this once:

with open("test1.txt", "w") as f:
    f.write("Apple\nBanana\nCherry")

🛑 Before Anything — Handle Missing Files

If the file does not exist and you try to open it, Python will crash immediately with a FileNotFoundError. Always wrap file reading in a try-except block.

try:
    file = open("missing_file.txt", "r")
    data = file.read()
    file.close()
except FileNotFoundError:
    print("Error: The file does not exist!")

✅ Output:

Error: The file does not exist!

This way your program keeps running instead of crashing. We will see a better solution at the end using with open().


🔹 1. read() — Read the Entire File at Once

read() reads every single character in the file and returns one big string. It is the simplest method.

file = open("test1.txt", "r")
data = file.read()
print(data)
print("Length:", len(data))
file.close()

✅ Output:

Apple
Banana
Cherry
Length: 19

⚠️ The Hidden Trap — Why Length is 19:

The file has 3 words: Apple (5) + Banana (6) + Cherry (6) = 17 characters. But read() also captures the invisible newline character \n at the end of each line. So:

"Apple\n"   → 6 characters
"Banana\n"  → 7 characters
"Cherry"    → 6 characters
Total       → 19 characters

This is why len(data) returns 19, not 17. Always remember — newlines are real characters.

📌 read(N) — Read Only N Characters

You can pass a number to read only that many characters:

file = open("test1.txt", "r")
print(file.read(5))    # Read first 5 characters only
print(file.read(3))    # Read next 3 characters
file.close()

✅ Output:

Apple
\nBa

Note: The cursor keeps moving forward. Second read(3) continues from where the first stopped — it does not restart.

💡 Real Scenario:

Reading an entire config file, a small text file, or loading template content into a variable.


🔹 2. readline() — Read One Line at a Time

readline() reads characters until it hits a \n, then stops. Calling it again reads the next line. The cursor moves line by line.

file = open("test1.txt", "r")

line1 = file.readline()
line2 = file.readline()
line3 = file.readline()
line4 = file.readline()    # Beyond last line

print(repr(line1))
print(repr(line2))
print(repr(line3))
print(repr(line4))         # What happens here?

file.close()

✅ Output:

'Apple\n'
'Banana\n'
'Cherry'
''

✅ Explanation:

  • Each call reads one line including the \n at the end
  • The last line Cherry has no \n so none is added
  • If you call readline() after the last line → returns empty string ''
  • We used repr() to see the hidden \n characters clearly

⚠️ The Double Blank Line Trap:

file = open("test1.txt", "r")
data = file.readline()
print(data)              # Prints "Apple\n" + print() adds another \n
print("---")
file.close()
Apple

---

There is an extra blank line! This is because readline() keeps the \n, and print() adds its own \n on top. Fix: use strip().

file = open("test1.txt", "r")
data = file.readline().strip()   # Remove trailing \n
print(data)
print("Length:", len(data))       # Now 5, not 6
file.close()
Apple
Length: 5

📌 Reading All Lines Using a Loop with readline():

file = open("test1.txt", "r")

while True:
    line = file.readline()
    if line == "":          # Empty string means end of file
        break
    print(line.strip())

file.close()
Apple
Banana
Cherry

💡 Real Scenario:

Processing a very large file line by line without loading it all into memory. Useful for GBs of log files.


🔹 3. readlines() — Get All Lines as a List

readlines() reads the entire file and puts each line into a Python list. Very convenient when you want to work with lines as individual items.

📌 Without strip() — The Raw Output:

file = open("test1.txt", "r")
data = file.readlines()
print(data)
print("Total items:", len(data))
file.close()

✅ Output:

['Apple\n', 'Banana\n', 'Cherry']
Total items: 3

Notice the \n at the end of each item (except the last). If you use this list in a program — for example comparing values — those hidden \n characters will cause bugs.

📌 With strip() — Clean Output:

file = open("test1.txt", "r")
data = file.readlines()

clean_data = [line.strip() for line in data]    # Remove \n from each line

print(clean_data)
print("Total lines:", len(clean_data))
file.close()

✅ Output:

['Apple', 'Banana', 'Cherry']
Total lines: 3

✅ Why strip() Works:

  • strip() removes whitespace from both ends of a string
  • \n is considered whitespace, so it gets removed
  • A list comprehension [line.strip() for line in data] applies it to every item at once

📌 Access Individual Lines by Index:

file = open("test1.txt", "r")
data = file.readlines()
file.close()

print(data[0].strip())    # First line
print(data[1].strip())    # Second line
print(data[-1].strip())   # Last line
Apple
Banana
Cherry

💡 Real Scenario:

Reading a CSV-style file, a list of usernames, or configuration options where each line is one item to process.


🔹 4. Iterating Directly Over the File Object

Python allows you to loop directly over a file without calling any read method. This is memory-efficient and clean.

file = open("test1.txt", "r")

for line in file:
    print(line.strip())

file.close()

✅ Output:

Apple
Banana
Cherry

This is often the preferred method for large files because Python reads one line at a time — it never loads the whole file into memory.


🔹 5. The with open() Statement — Best Practice

Forgetting file.close() causes memory leaks and can corrupt files. The with open() statement solves this permanently — it automatically closes the file when the block ends, even if an error occurs.

# read() with with open
with open("test1.txt", "r") as file:
    data = file.read()
    print(data)
Apple
Banana
Cherry
# readlines() with with open — clean version
with open("test1.txt", "r") as file:
    clean_data = [line.strip() for line in file.readlines()]
    print(clean_data)
['Apple', 'Banana', 'Cherry']
# Loop line by line with with open — most memory efficient
with open("test1.txt", "r") as file:
    for line in file:
        print(line.strip())
Apple
Banana
Cherry

Rule of thumb: Always use with open(). There is no reason to use the old file.close() pattern in modern Python.


🔹 6. Reading with encoding — Handle Special Characters

If your file contains special characters (Hindi, Arabic, emojis, accented letters), you must specify the encoding or Python may crash with a UnicodeDecodeError.

with open("test1.txt", "r", encoding="utf-8") as file:
    data = file.read()
    print(data)

UTF-8 is the standard encoding used by most modern systems. Always specify it to be safe.


🔥 Comparison Table — Which Method to Use When?

Method Returns Best For Memory
read() One big string Small files, full content needed Loads all at once
read(N) N characters as string Reading specific chunks Efficient
readline() One line as string Reading line by line manually Very efficient
readlines() List of all lines When you need all lines as a list Loads all at once
for line in file One line per iteration Large files, looping all lines Most efficient

🚀 Pro Tips

  • Always use with open() — never forget file.close() again
  • Always call .strip() on lines to remove hidden \n characters
  • Use encoding="utf-8" whenever your file may have special characters
  • For large files (GBs), always prefer looping directly over the file object — never read() the whole thing
  • Use repr(line) while debugging to see hidden \n, \t characters visually

❌ Common Mistakes

  • Forgetting file.close() → causes memory leaks and potential file corruption
  • Not using strip() on lines\n silently breaks string comparisons
  • Reading a file twice without seek(0) → second read() returns empty string because cursor is at end
  • Not handling FileNotFoundError → program crashes if file is missing
  • Using readlines() on a multi-GB file → loads everything into RAM and causes slowdowns or crash

📌 The seek(0) Gotcha:

file = open("test1.txt", "r")

data1 = file.read()
data2 = file.read()      # ❌ Returns empty string! Cursor is at end

print(repr(data2))       # ''

file.seek(0)             # ✅ Rewind cursor to start
data3 = file.read()
print(data3)             # Works now!

file.close()
''
Apple
Banana
Cherry

🔚 Conclusion

Python gives you multiple ways to read files — each suited for different situations:

  • read() → grab everything at once as a string
  • read(N) → grab exactly N characters
  • readline() → one line at a time, cursor moves forward
  • readlines() → all lines as a list (use strip() to clean)
  • for line in file → most efficient, best for large files

Master these methods and you will be able to process any file Python throws at you ⚡

Post a Comment

Do Leave Your Comments...

Previous Post Next Post

Contact Form