File IO

File I/O (Input/Output) operations are crucial for interacting with external files. Python provides a simple and powerful interface for reading from and writing to files.

Opening Files

In Python, you open a file using the built-in open() function, which returns a file object. The syntax is:

file_object = open(file_name, mode)

Parameters:

  • file_name: Name of the file or path to the file.
  • mode: Specifies the mode in which the file should be opened.

File Modes

  • ‘r’: Read (default). Raises an error if the file doesn’t exist.
  • ‘w’: Write. Creates a new file or truncates the existing file.
  • ‘a’: Append. Creates a new file or appends to the existing file.
  • ‘b’: Binary mode (used for non-text files like images).
  • ‘+’: Read and write mode.
# Opening a file for reading
file = open('example.txt', 'r')

# Closing the file
file.close()

Reading Files

Once a file is open, there are multiple ways to read its content.

read(): Reads the entire file

file = open('example.txt', 'r')
content = file.read()

print(content)
file.close()

readline(): Reads one line at a time

file = open('example.txt', 'r')
line = file.readline()

print(line)
file.close()

readlines(): Reads all lines into a list

file = open('example.txt', 'r')
lines = file.readlines()

print(lines)
file.close()

Important: Always remember to close the file after you’re done to free system resources. Alternatively, use a context manager to automatically close the file.

Writing to Files

You can write data to a file using the write() method.

file = open('example.txt', 'w')
file.write('Hello, world!')
file.close()

If the file doesn’t exist, it will be created. If it does exist, its contents will be overwritten.

Appending to Files

To append data to an existing file without overwriting it, use the ‘a’ mode.

file = open('example.txt', 'a')
file.write('This line will be appended.\n')
file.close()

Using with Statement (Context Managers)

A better way to handle files in Python is by using the with statement, which automatically handles closing the file for you, even if an exception is raised:

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

Using a context managereliminates the need to explicitly call file.close(). The file is closed as soon as the block inside the with statement is exited.

Working with Binary Files

For binary data (like images or executable files), open the file in binary mode by using the ‘b’ flag:

# Reading a binary file (e.g., an image)
with open('image.png', 'rb') as file:
    binary_content = file.read()
    print(binary_content)

Similarly, you can write binary data to a file:

with open('output.bin', 'wb') as file:
    file.write(b'\x00\xFF\x00\xFF')

Handling File Paths

Python’s os and pathlib modules provide functions for working with file paths across different operating systems.

Using os.path:

import os

# Joining paths
file_path = os.path.join('folder', 'subfolder', 'file.txt')

# Checking if a file exists
if os.path.exists(file_path):
    print("File exists.")

Using pathlib (introduced in Python 3.4):

from pathlib import Path

file_path = Path('folder/subfolder/file.txt')

# Checking if file exists
if file_path.exists():
    print("File exists.")

pathlib offers an object-oriented approach to handling file paths and is generally more flexible than os.path.

Advanced File I/O Concepts

  • File Iteration: You can iterate over a file object line by line, which is memory-efficient for large files:
with open('large_file.txt', 'r') as file:
    for line in file:
        print(line.strip())  # Reads file line by line
  • File Seeking: You can move the file pointer using seek() and get the current pointer position with tell():
with open('example.txt', 'r') as file:
    file.seek(5)  # Move to the 6th byte (0-indexed)
    print(file.read())  # Reads from the 6th byte onwards
  • File Buffers: For large I/O operations, you can read or write data in chunks:
with open('large_file.txt', 'rb') as file:
    chunk_size = 1024  # Read in 1 KB chunks
    while chunk := file.read(chunk_size):
        process_chunk(chunk)

Track your progress

Mark this subtopic as completed when you finish reading.