100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Reading and Writing Files in Python

Explore the different methods for reading and writing file content, including read(), readline(), readlines(), write(), and writelines().

Modules, Files & IterablesBeginner8 min readJul 7, 2026
Analogies

1. Introduction

Once a file is open, Python gives you several methods to read or write its contents depending on whether you want the whole file at once, one line at a time, or all lines as a list. Choosing the right method affects both memory usage and how easy the resulting code is to work with.

🏏

Cricket analogy: Choosing read() versus readline() versus readlines() is like choosing to read a full match scorecard at once, ball-by-ball commentary one line at a time, or every over's summary as a list -- each suits a different review need.

For writing, Python offers write() for a single string and writelines() for a sequence of strings, giving you flexibility over how data is produced.

🏏

Cricket analogy: write() is like scribbling a single note on the scorecard, while writelines() is like pasting an entire pre-written list of over summaries onto the card in one motion, giving the scorer flexibility in how entries are added.

2. Syntax

python
# Reading
f.read()          # entire file as one string
f.read(n)         # up to n characters/bytes
f.readline()       # a single line, including the trailing newline
f.readlines()      # a list of all lines

# Writing
f.write('text')          # write a single string
f.writelines(['a\n', 'b\n'])  # write a list of strings (no automatic newlines added)

# Iterating line by line
for line in f:
    print(line)

3. Explanation

read() loads the entire file into memory as a single string, which is convenient for small files but wasteful for very large ones. readline() reads just the next line (including its trailing '\n'), while readlines() returns every line as a list of strings, each still containing its newline character.

🏏

Cricket analogy: read() is like reading the entire match scorecard into your head at once, fine for a single T20 but overwhelming for a five-day Test archive; readline() reads just the next over's line; readlines() lists every over's line including its line break.

writelines() writes each string in the given iterable one after another; unlike print(), it does not add newline characters automatically, so your input strings must already include '\n' where line breaks are needed.

🏏

Cricket analogy: writelines() pastes each over-summary string one after another exactly as given, unlike a scoreboard announcer who automatically adds a pause between overs -- so each string must already include its own line-break marker.

Both readline() and readlines() keep the trailing '\n' on each line. A common pattern is to call line.strip() (or .rstrip('\n')) when printing or processing lines to remove that trailing newline.

4. Example

python
import tempfile, os

path = os.path.join(tempfile.gettempdir(), 'demo_read_write.txt')

f = open(path, 'w')
f.writelines(['apple\n', 'banana\n', 'cherry\n'])
f.close()

f = open(path, 'r')
line1 = f.readline()
rest = f.readlines()
f.close()

print(line1.strip())
for line in rest:
    print(line.strip())

os.remove(path)

5. Output

text
apple
banana
cherry

6. Key Takeaways

  • read() returns the whole file as one string; readlines() returns a list of lines; readline() returns one line at a time.
  • writelines() writes an iterable of strings without adding newlines automatically.
  • Lines from readline()/readlines() retain their trailing '\n'; use strip() to remove it.
  • Iterating directly over a file object ('for line in f') reads it lazily, line by line, which is memory-efficient for large files.
  • Reading and writing should be paired with proper closing (or 'with') to avoid resource leaks.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#ReadingAndWritingFilesInPython#Reading#Writing#Files#Syntax#StudyNotes#SkillVeris