Python101 — Python File I/O: A Comprehensive Guide

Niraj Tiwari
6 min readMar 23, 2024

--

Welcome to the world of Python File I/O (Input/Output)! This guide aims to introduce us to the fundamentals of working with files in Python. By the end of this journey, we’ll be able to read from, write to, and manage files efficiently in our Python programs. Let’s get started!

What is File I/O?

File Input/Output, commonly referred to as File I/O, involves reading data from and writing data to files. It is a critical aspect of programming since it allows data to persist beyond the execution of the program.

How do we open a file in Python for reading and writing?

# For reading
file = open("example.txt", "r")

# For writing
file = open("example.txt", "w")

What are the different modes for opening a file in Python?

In Python, when we open a file using the `open()` function, we can specify different modes for different operations. Here are the common modes available:

- r (Read mode): Opens the file for reading. This is the default mode if no mode is specified.
- w (Write mode): Opens the file for writing. Creates a new file if it does not exist or truncates (empties) the file if it exists.
- a (Append mode): Opens the file for appending. Any data written to the file is automatically added to the end. It creates a new file if it does not exist.
- r+ (Read and Write mode): Opens the file for both reading and writing. The file pointer is placed at the beginning of the file.
- w+: Opens the file for both writing and reading. Like `’w’`, it creates a new file or truncates the existing file.
- a+ (Append and Read mode): Opens the file for reading and appending. Like `’a’`, it creates a new file if it does not exist. The file pointer is at the end of the file if the file exists.
- b (Binary mode): This mode is used with other modes (e.g., `’rb’`, `’wb’`, `’ab+’`) for reading, writing, or appending in binary format. This is useful for non-text files like images or executable files.
- t (Text mode): This is the default mode and can be combined with other modes like `’rt’`, `’wt+’`, etc. It indicates the file is a text file.

We can combine these modes with binary or text modes by appending b or t to the mode string, although t is the default. For example, rb opens a file in binary read mode, and w+t opens a file for reading and writing in text mode.

How do we write or read data from a file in Python?

A more Pythonic way to handle files is by using the with statement. It ensures that the file is properly closed once the block of code is executed, even if an error occurs. It's cleaner and more concise:

with open("example.txt", "w") as file:
file.write("Hello, Python!")
with open("example.txt", "r") as file:
content = file.read()
print(content)

No need to explicitly close the file — it’s automatically done for us.

ou can read a file in several ways:

  • read(): Reads the entire file.
  • readline(): Reads the next line from the file.
  • readlines(): Reads all the lines and returns them as a list.
# Reading the entire file
with open('example.txt', 'r') as file:
content = file.read()
print(content)

# Reading line by line
with open('example.txt', 'r') as file:
line = file.readline()
while line:
print(line.strip()) # strip() removes the newline character
line = file.readline()

What is the difference between reading a file line by line and reading the entire file at once?

Reading line by line is more memory efficient, especially for large files.

# Line by line
with open("example.txt", "r") as file:
for line in file:
print(line.strip())

# Entire file
with open("example.txt", "r") as file:
content = file.read()
print(content)

How do we handle errors when working with files in Python?

try:
with open("example.txt", "r") as file:
content = file.read()
print(content)
except FileNotFoundError:
print("File does not exist.")

How do we check if a file exists before opening it in Python?

import os
if os.path.exists("example.txt"):
with open("example.txt", "r") as file:
print(file.read())
else:
print("The file does not exist.")

How do we create a new file in Python?

with open("newfile.txt", "w") as file:
file.write("New file content")

How do we delete a file in Python?

import os
os.remove("newfile.txt")

How do we move the cursor position within a file in Python?

with open("example.txt", "r") as file:
file.seek(10) # Move the cursor to the 11th byte
print(file.read())

How do we read and write binary files in Python?

# Write binary
with open("binary.dat", "wb") as file:
file.write(b'\x00\xFF') # Writing bytes

# Read binary
with open("binary.dat", "rb") as file:
content = file.read()
print(content)

How do we work with CSV files in Python?

import csv

# Reading a CSV file
with open("example.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)

# Writing to a CSV file
with open("example.csv", "w", newline='') as file:
writer = csv.writer(file)
writer.writerow(["name", "age"])
writer.writerow(["John", "22"])

How do we handle encoding and decoding when working with files in Python?

# Writing with specific encoding
with open("example.txt", "w", encoding="utf-8") as file:
file.write("Some text with unicode characters like ü")

# Reading with specific encoding
with open("example.txt", "r", encoding="utf-8") as file:
print(file.read())

How do we read and write JSON files in Python?

For handling JSON files, Python provides a module named json that can be used to read from and write to JSON files.

Writing to a JSON file:

import json

data = {'name': 'John Doe', 'age': 29, 'city': 'New York'}

# Writing JSON data
with open('data.json', 'w') as json_file:
json.dump(data, json_file)

Reading from a JSON file:

import json

# Reading JSON data
with open('data.json', 'r') as json_file:
data = json.load(json_file)
print(data)

How do we iterate over files in a directory using Python?

To iterate over files in a directory, we can use the os module, which provides a way to work with the file system, including listing directory contents.

import os

directory = '/path/to/directory'

# Iterating over files in a directory
for filename in os.listdir(directory):
if filename.endswith('.txt'): # or any file extension
print(filename)
# Further processing here

How do we copy the contents of one file to another in Python?

Copying content from one file to another can be easily done using the built-in shutil module or by reading from one file and writing to another manually.

Using shutil module:

import shutil

# Copying file
shutil.copyfile('source.txt', 'destination.txt')

Manually copying file contents:

# Manually copying file contents
with open('source.txt', 'r') as source_file:
content = source_file.read()

with open('destination.txt', 'w') as destination_file:
destination_file.write(content)

How do we append data to an existing file in Python?

Appending data to an existing file involves opening the file in append mode ('a') and writing the new data. This mode ensures that the existing content is not overwritten.

# Appending data to an existing file
with open('example.txt', 'a') as file:
file.write("\nNew data appended.")

These snippets provide a basic foundation for working with JSON files, iterating over directory contents, copying files, and appending data in Python.

Optimizing File I/O Performance for Large Datasets

For handling large datasets efficiently, consider using buffered reading and writing, processing data in chunks, or employing lazy loading with generators.

def read_in_chunks(file_object, chunk_size=1024):
"""Lazy function to read a file piece by piece."""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data

with open('large_dataset.txt', 'r') as f:
for piece in read_in_chunks(f):
process_data(piece)

Working with File Metadata

We can use the os and os.path modules to access file metadata.

import os
file_stats = os.stat('example.txt')
print(f"File size: {file_stats.st_size} bytes")

Advantages of the os Module

The os module provides a portable way of using operating system-dependent functionality like creating, removing directories, fetching environment variables, and more.

import os
# Create a directory
os.makedirs('new_directory', exist_ok=True)

Handling Different Line Endings

Python’s universal newlines mode automatically translates different line endings to \n.

with open('example.txt', 'r', newline=None) as file:
content = file.read()

--

--

Niraj Tiwari

AI | ML | DL | Large Scale Systems | Systems Thinking | Human Behaviour | Psychology | NLP | Team Building | Leadership