· Python · 3 min read

Writing Files to Disc Efficiently with Python

Writing Files to Disc Efficiently with Python

Introduction

Writing files to disc is an important and common task for many Python developers. It’s essential for tasks like writing log files, saving data, or generating output files. In this article, we will explore different ways to write files to disc effectively and give useful tips on achieving optimal performance and reliable data storage using Python.

Properties and Parameters

When writing files in Python, primarily two functions are used: open() and write(). The open() function opens a file and returns a file object which can be used to write data to the file.

The open() function accepts essential parameters:

  • file: The file name or the path to the file.
  • mode: It’s an optional parameter that indicates the file mode. The default mode is 'r' for reading. To write files, the mode 'w' should be used. Other modes include 'a' (append), 'x' (exclusive creation), 'b' (binary), and 't' (text).
  • encoding: The encoding format to use when working with text files. The default value is platform dependent, but generally 'utf-8' encoding is used.

After opening the file, the write() function is used to write data to the file. The write() function accepts one parameter:

  • data: The data to be written to the file. It should be a string for text files, and bytes for binary files.

Here’s an example of opening a file and writing data to it:

file = open("output.txt", "w")
file.write("This is a test.")
file.close()

In the example, a file called “output.txt” is opened in 'w' mode for writing text, the data is written, and the file is closed.

Basic Example

Let’s demonstrate a simple example of writing lines of text to a file:

lines = ['Line 1\n', 'Line 2\n', 'Line 3\n']

with open('basic_example.txt', 'w') as file:
    for line in lines:
        file.write(line)

In this example, we use a with statement to automatically close the file when the block is done. The write() function is called for each line to write data to the file.

Advanced Example

Consider a more complex example, where you’re dealing with large datasets in CSV format and need to filter the data before writing it to a new CSV file.

import csv

# Read the input CSV file and filter data
def read_and_filter_data(input_file):
    filtered_data = []
    with open(input_file, 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            if int(row[2]) > 50:
                filtered_data.append(row)
    return filtered_data

# Write a new CSV file with filtered data
def write_filtered_data(output_file, filtered_data):
    with open(output_file, 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(['Name', 'Address', 'Score'])
        for row in filtered_data:
            writer.writerow(row)

input_file = 'input_data.csv'
output_file = 'filtered_data.csv'

filtered_data = read_and_filter_data(input_file)
write_filtered_data(output_file, filtered_data)

In this example, we first read input_data.csv, filter rows where the score is greater than 50, and then write filtered data to filtered_data.csv. We use Python’s csv module for reading and writing CSV files.

Personal Tips

  1. Always use the with statement for opening files, as it ensures that the file is closed after its usage. This is important to avoid file locking issues and resource leakage.

  2. When working with very large files, consider using buffered I/O for writing the file or writing the file in chunks. This will help to optimize memory usage.

  3. For binary and encoded data, consider using the 'wb' (write binary) mode or 'ab' (append binary) mode in the open() function.

  4. When dealing with multiple files, consider using os.path.join() to join parts of a path. This ensures compatibility across different operating systems.

  5. Make sure to handle errors and exceptions when performing file operations. Use try-except blocks to gracefully handle these situations and provide meaningful error messages.

Share: