File Modes

Python · File Handling

Theory

The second argument to open() is the mode, a short string that sets what you can do with the file and what happens to it on opening. 'r' reads (the default; the file must exist). 'w' writes, creating the file or erasing its contents. 'a' appends to the end, creating the file if needed. 'x' creates a new file and fails if it already exists. Adding '+' allows both reading and writing: 'r+' reads and writes without erasing, while 'w+' erases first.

Adding 'b' switches from text to binary mode: 'rb' and 'wb' work with raw bytes instead of strings, with no encoding or newline translation. Binary is needed for images, PDFs, Excel files and pickles. So open('data.csv', 'r') reads text, open('photo.png', 'rb') reads bytes and open('log.txt', 'a') appends text.

Why we use it

Choosing the right mode prevents nasty surprises, like wiping a file with 'w' when you meant to add to it, or corrupting an image by opening it as text.

When to use it

Ask two questions: read, write or append? Text or binary? Use 'r' to read, 'a' to keep history, 'w' only when replacing is intended, 'x' to avoid accidental overwrites, and add 'b' for any non-text file.

How it works

  1. Choose a mode for open()
  2. Only reading? Yes: Use 'r' (or 'rb' for binary). No: Decide how to write.
  3. Keep existing content? Yes: 'a' appends, 'r+' edits. No: 'w' replaces, 'x' refuses to overwrite.
  4. Is the file non-text (image, PDF)? Yes: Add 'b', e.g. 'wb'. No: Stay in text mode.
  5. Open the file with the chosen mode

Syntax

open(path, mode)
# 'r' 'w' 'a' 'r+' 'rb' 'wb'

Example

open('data.csv', 'r')      # read text
open('data.bin', 'rb')     # read binary
open('log.txt', 'a')       # append text

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic