Skip to content

Computer Science · Ch 2 — File Handling in Python

Opening a file

2.3.1

Opening a file

To work with a file in Python, the first step is to open it. You do this using the open() function, which returns a file object (also called a file handle). This file object is stored in a variable, and that variable is what you use to actually read data from the file or write data to it.

The general syntax is:

file_object = open(file_name, access_mode)

The file_object variable now holds the connection to the file. Through it, you can call functions from Python’s io module to transfer data. If the file you name does not already exist, open() will create a new, empty file with that name.

Once a file is opened, the file object has three useful attributes that tell you its current state:

  • file.closed — returns True if the file is closed, False otherwise.
  • file.mode — returns the access mode the file was opened with (e.g., 'r', 'w').
  • file.name — returns the name of the file.

The file_name argument is simply the name of the file you want to open. If the file is not in the same folder as your program (the current working directory), you must provide the full path to it.

The access_mode argument is optional. It tells Python what you intend to do with the file — read, write, append, and so on. This is also called the processing mode. If you don’t specify a mode, the default is read mode ('r').

The basic mode characters are:

  • 'r' — read
  • 'w' — write
  • 'a' — append (add data to the end of an existing file)
  • '+' — both reading and writing

You can also specify whether the file should be handled as text or binary. By default, files are opened in text mode, meaning you read and write strings. For non-textual data (like images or audio), you open the file in binary mode by adding 'b' to the mode string. In binary mode, data is read and written as bytes.

The table below lists the common file open modes and what they do. The file offset position refers to where the file object’s pointer starts when the file is opened.

File ModeDescriptionFile Offset Position
'r'Opens the file for reading only.Beginning of the file
'rb'Opens the file in binary mode for reading only.Beginning of the file
'r+' or '+r'Opens the file for both reading and writing.Beginning of the file
'w'Opens the file for writing. If the file already exists, all its contents are erased (overwritten). If it doesn’t exist, a new file is created.Beginning of the file
'wb+' or '+wb'Opens the file for reading, writing, and in binary mode. Overwrites existing content or creates a new file.Beginning of the file
'a'Opens the file for appending. If the file doesn’t exist, a new file is created.End of the file
'a+' or '+a'Opens the file for appending and reading. If the file doesn’t exist, a new file is created.End of the file
Table 2.1File Open Modes
File ModeDescriptionFile Offset position
<r>Opens the file in read-only mode.Beginning of the file
<rb>Opens the file in binary and read-only mode.Beginning of the file
<r+> or <+r>Opens the file in both read and write mode.Beginning of the file
<w>Opens the file in write mode. If the file already exists, all the contents will be overwritten. If the file doesn't exist, then a new file will be created.Beginning of the file
<wb+> or <+wb>Opens the file in read,write and binary mode. If the file already exists, the contents will be overwritten. If the file doesn't exist, then a new file will be created.Beginning of the file