File Modes in Python

  • This lesson teaches the different modes used when opening files in Python.

  • What are File Modes?

    File modes tell Python how a file should be opened and what operations are allowed on that file.

    Using file modes, we can:

    • Read data from a file

    • Write data to a file

    • Append data to a file

    • Work with text or binary files

    Syntax for File Modes

    file = open("filename", "mode")

    Example:

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

    Types of File Modes in Python

    Python supports text modes and binary modes.

    1️.Text File Modes

    Text mode is the default mode in Python.

    r – Read Mode

    Purpose:

    • Opens file for reading

    • File must exist

    • Cursor starts at beginning

    Syntax:

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

Reading File Using Read Mode

Reads and displays file content.

file = open("data.txt", "r")
print(file.read())
file.close()
  • w – Write Mode

    Purpose:

    • Opens file for writing

    • Overwrites existing content

    • Creates new file if not exists

    Old data will be deleted.

    Syntax:

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

Writing Data Using Write Mode

Writes new text into file.

file = open("data.txt", "w")
file.write("Welcome to Python")
file.close()
  •  a – Append Mode

    Purpose:

    • Adds data at end of file

    • Does not delete old content

    • Creates file if not exists

    Syntax:

    open("file.txt", "a")

Appending Data Using Append Mode

Adds new content without removing old data.

file = open("data.txt", "a")
file.write("\nThis is appended text")
file.close()
  • x – Exclusive Creation Mode

    Purpose:

    • Creates a new file

    • Throws error if file already exists

    Syntax:

    open("file.txt", "x")

Creating File Using x Mode

Creates a file only if it does not exist.

file = open("newfile.txt", "x")
file.write("New file created")
file.close()
  • 2️.Read & Write Combined Modes

    r+ – Read and Write Mode

    Purpose:

    • Read and write both

    • File must exist

    • Cursor at beginning

    open("file.txt", "r+")

    w+ – Write and Read Mode

    Purpose:

    • Write + read

    • Deletes old content

    • Creates file if not exists

    open("file.txt", "w+")

    a+ – Append and Read Mode

    Purpose:

    • Append + read

    • Cursor at end

    • Old data remains safe

    open("file.txt", "a+")

    3️.Binary File Modes

    Binary modes are used for:

    • Images

    • Audio files

    • Videos

    • PDFs

    rb – Read Binary

    open("image.jpg", "rb")

    wb – Write Binary

    open("image.jpg", "wb")

    ab – Append Binary

    open("image.jpg", "ab")

Reading Binary File

Reads binary data from image file.

file = open("photo.jpg", "rb")
data = file.read()
file.close()
  • Summary Table of File Modes

    Mode

    Purpose

    File Exists?

    Data Lost?

    r

    Read

    Required

    w

    Write

    Optional

    a

    Append

    Optional

    x

    Create

    Must NOT exist

    r+

    Read + Write

    Required

    w+

    Write + Read

    Optional

    a+

    Append + Read

    Optional

    rb

    Read Binary

    Required

    wb

    Write Binary

    Optional