Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Read Files (Complete Guide for Beginners)

 Reading files is one of the most important operations in Python file handling. It allows you to access and display data stored in a file so your program can process or analyze it.

Python provides multiple methods to read files easily, such as read(), readline(), and readlines().

In this tutorial, you will learn how to read files in Python with clear examples.


What is Reading a File?

Reading a file means opening a stored file and retrieving its content into a Python program.

You can read:

  • Entire file at once
  • Line by line
  • Multiple lines as a list

File Open Mode for Reading

ModeDescription
'r'Read mode (default)

1. Reading Entire File (read())

The read() method reads the full content of a file.

Example:

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

content = file.read()

print(content)

file.close()

Explanation:

  • Opens file in read mode
  • Reads all content at once
  • Returns a single string

2. Reading File Line by Line (readline())

The readline() method reads one line at a time.

Example:

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

print(file.readline())
print(file.readline())

file.close()

Output:

Line 1
Line 2

3. Reading All Lines (readlines())

The readlines() method reads all lines and returns a list.

Example:

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

lines = file.readlines()

print(lines)

file.close()

Output:

['Line 1\n', 'Line 2\n', 'Line 3\n']

4. Reading File Using Loop

You can loop through a file directly.

Example:

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

for line in file:
    print(line)

file.close()

Explanation:

  • Reads file line by line
  • Memory efficient method

5. Reading File Using with Statement (Best Practice)

The with statement automatically closes the file.

Example:

with open("example.txt", "r") as file:
    content = file.read()
    print(content)

6. Reading Large Files Efficiently

For large files, use line-by-line reading.

Example:

with open("bigfile.txt", "r") as file:
    for line in file:
        print(line.strip())

7. Checking File Before Reading

Example:

import os

if os.path.exists("example.txt"):
    with open("example.txt", "r") as file:
        print(file.read())
else:
    print("File not found")

Real-World Example: Reading Student Data

with open("students.txt", "r") as file:
    for line in file:
        print("Student Record:", line.strip())

Real-World Example: Reading Log File

with open("log.txt", "r") as file:
    logs = file.readlines()

for log in logs:
    print(log.strip())

Difference Between Reading Methods

MethodDescription
read()Reads entire file
readline()Reads one line
readlines()Reads all lines into list
loopReads line by line
with open()Safe file reading

Common Mistakes

Mistake 1: Forgetting to close file

❌ Wrong:

file = open("example.txt", "r")
print(file.read())

✔ Always close file or use with


Mistake 2: Using wrong mode

open("example.txt", "w")

✔ This overwrites file instead of reading


Mistake 3: Reading large file with read()

✔ Can cause memory issues


Safe Reading Example

with open("example.txt", "r") as file:
    for line in file:
        print(line.strip())

Conclusion

Reading files in Python is essential for working with stored data.

You learned:

  • How to read full files and lines
  • Different reading methods
  • Safe file handling using with
  • Real-world applications like logs and student records

Mastering file reading helps you build powerful applications like data analysis tools, log systems, and automation scripts.




Post a Comment

0 Comments