Fundamentals 12 min read

Master Python File I/O: Reading, Writing, and Mode Pitfalls Explained

This guide walks through Python file I/O by comparing manual Windows text handling to code, explains file handles, demonstrates reading and writing with various modes, clarifies read vs. readline vs. readlines, and shows how to efficiently process large files without exhausting memory.

ITPUB
ITPUB
ITPUB
Master Python File I/O: Reading, Writing, and Mode Pitfalls Explained

1. File Read/Write Process

Just as you would locate, open, view, and close a Word document in Windows, Python works with a file handle that represents the opened file in memory.

Steps in Windows

Find the document

Open it

View or edit its contents

Close it

Steps in Python

Obtain a file handle using open() Perform operations (read, write, etc.) through the handle

Close the file with

close()

2. What Is a File Handle?

A file handle stores the file name, encoding, size, and its starting position on disk. After opening a file, the handle points to the file’s data in memory, allowing subsequent read or write operations.

3. Demonstrating File Reading

f = open(r"G:\6Tipdm\file_read_write\yesterday.txt", "r", encoding="utf-8")
data = f.read()
print(data[:245])
f.close()

The code reads the file and prints the first 245 characters. A second attempt to read the same handle returns an empty string because the handle’s cursor is already at the end of the file. Resetting the cursor with f.seek(0) allows the file to be read again.

4. Demonstrating File Writing

f = open(r"G:\6Tipdm\file_read_write\yesterday2.txt", "w", encoding="utf-8")
f.write("我爱北京天安门")
f.close()

Writing overwrites the file. Adding more write() calls without resetting the cursor replaces previous content because the file is opened in write mode, which truncates the file on each open.

5. Common File Modes

r+ (read/write, file must exist)

# read only
f = open(r"...\yesterday1.txt", "r+", encoding="utf-8")
print(f.read())
f.close()

# write then read without resetting cursor
f = open(r"...\yesterday1.txt", "r+", encoding="utf-8")
f.write("丽丽姑娘")
print(f.read())  # empty because cursor is at end

# write then reset cursor and read
f = open(r"...\yesterday1.txt", "r+", encoding="utf-8")
f.write("丽丽姑娘")
f.seek(0)
print(f.read())
f.close()

Reading after writing fails unless seek(0) moves the cursor back to the file start.

w+ (read/write, creates file if missing, truncates on open)

# write then attempt to read
f = open(r"...\yesterday3.txt", "w+", encoding="utf-8")
f.write("bbbbbb")
print(f.read())  # empty because file was truncated and cursor at end

# write, reset, then read
f = open(r"...\yesterday3.txt", "w+", encoding="utf-8")
f.write("哈哈哈哈哈")
f.seek(0)
print(f.read())
f.close()

In w+ mode the file is cleared on open; you must reset the cursor before reading.

a+ (append/read, cursor starts at end)

# append then read without resetting
f = open(r"...\yesterday4.txt", "a+", encoding="utf-8")
f.write("哈哈")
f.close()

f = open(r"...\yesterday4.txt", "a+", encoding="utf-8")
print(f.read())  # empty because cursor is at end

f.seek(0)
print(f.read())  # now shows content
f.close()

Appending positions the cursor at the file’s end; you must call seek(0) to read existing data.

6. read vs. readline vs. readlines

# read() returns the whole file as a single string
f = open(r"...\test.txt", "r", encoding="utf-8")
content = f.read()
print(type(content))
print(content)
f.close()

# readline() returns one line at a time
f = open(r"...\test.txt", "r", encoding="utf-8")
line = f.readline()
print(type(line))
print(line)
for _ in range(3):
    print(f.readline())
f.close()

# readlines() returns a list of all lines
f = open(r"...\test.txt", "r", encoding="utf-8")
lines = f.readlines()
print(type(lines))
print(lines)
f.close()
read()

loads the entire file into memory, readline() fetches one line per call, and readlines() returns a list of all lines.

7. Efficiently Viewing a Large (10 GB) File

f = open(r"G:\6Tipdm\file_read_write\yesterday.txt", "r", encoding="utf-8")
for line in f:
    print(line.strip())
f.close()

Iterating over the file object treats it as an iterator; each loop reads one line into memory, processes it, and discards it, keeping memory usage low regardless of file size.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonIteratorfile-iomemory-managementRead/Writefile-modes
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.