File I/O (Input/Output) is a fundamental part of programming, enabling data storage and retrieval for many applications. In Python, file I/O operations are straightforward to learn and powerful in functionality, allowing us to read from and write to files with minimal code.
In this article, we’ll cover:
- Opening files and file modes.
- Reading from files.
- Writing to files.
- Working with different file formats.
- Managing file resources with
with
statements.
Let’s dive in!
Opening Files and File Modes
To perform any operation on a file, we first need to open it. Python provides the open()
function for this purpose, which returns a file object. The open()
function requires at least one argument, the file name, and optionally takes a second argument, the mode.
File Modes in Python
The most common file modes include:
"r"
(Read Mode): Opens a file for reading. This is the default mode. The file must exist; otherwise, it raises an error."w"
(Write Mode): Opens a file for writing. If the file exists, it truncates (empties) it before writing. If the file doesn’t exist, it creates a new one."a"
(Append Mode): Opens a file for appending. Data is added to the end of the file without truncating it. If the file doesn’t exist, it creates a new one."r+"
(Read and Write Mode): Opens a file for both reading and writing. The file must exist."w+"
(Write and Read Mode): Opens a file for writing and reading. If the file exists, it’s truncated; otherwise, a new file is created."a+"
(Append and Read Mode): Opens a file for appending and reading. If the file doesn’t exist, it creates a new one.
Syntax Example
file = open("example.txt", "r") # Open file in read mode
To close the file after operations, we use file.close()
. However, using with
statements is a best practice for handling files, as it ensures files are properly closed even if an error occurs (more on this later).
Reading the Entire File
To read the entire content of a file at once, use the read()
method.
with open("example.txt", "r") as file:
content = file.read()
print(content)
Reading Line by Line
The readline()
method reads one line at a time, which is useful for large files.
with open("example.txt", "r") as file:
line = file.readline()
while line:
print(line, end='') # `end=''` avoids adding extra new lines
line = file.readline()
Reading All Lines into a List
Using readlines()
, we can read all lines and store them in a list.
with open("example.txt", "r") as file:
lines = file.readlines()
for line in lines:
print(line, end='')
Writing to Files
Writing data to a file can be done with the write()
and writelines()
methods. Writing operations require opening the file in a mode that allows writing, such as "w"
, "a"
, "w+"
, or "a+"
.
Writing a String to a File
with open("example.txt", "w") as file:
file.write("Hello, world!\n")
file.write("This is a new line.\n")
Appending Data to a File
To append data without overwriting the existing content, use "a"
mode.
with open("example.txt", "a") as file:
file.write("This line is appended.\n")
Writing Multiple Lines
The writelines()
method takes a list of strings and writes them to the file. Each list item should include a newline character (\n
) if you want each to start on a new line.
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open("example.txt", "w") as file:
file.writelines(lines)
Working with Different File Formats
Python’s file I/O capabilities aren’t limited to plain text files. The open()
function can also handle various formats, such as:
Working with CSV Files
For handling CSV files, Python provides the csv
module. Here’s a quick example of reading from and writing to CSV files.
Reading a CSV File
import csv
with open("data.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
Writing to a CSV File
import csv
data = [
["Name", "Age", "City"],
["Alice", 30, "New York"],
["Bob", 25, "Los Angeles"]
]
with open("data.csv", "w", newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
Working with JSON Files
For JSON files, Python provides the json
module.
Reading a JSON File
import json
with open("data.json", "r") as file:
data = json.load(file)
print(data)
Writing to a JSON File
import json
data = {"name": "Alice", "age": 30, "city": "New York"}
with open("data.json", "w") as file:
json.dump(data, file)
Managing File with with
Statements
Using with
statements for file I/O is a best practice because it automatically closes the file, even if an error occurs within the block.
with open("example.txt", "r") as file:
content = file.read()
print(content)
# The file is automatically closed after the `with` block
Without with
, you would need to explicitly close the file using file.close()
, which increases the risk of leaving files open if an error occurs before closing.
Handling Exceptions in File I/O
Errors in file I/O are common, such as attempting to open a file that doesn’t exist or trying to write to a read-only file. To handle these cases, Python offers the try
-except
blocks.
try:
with open("nonexistent.txt", "r") as file:
content = file.read()
except FileNotFoundError:
print("The file does not exist.")
except IOError:
print("An error occurred while accessing the file.")
Example: Complete File I/O Operation
Here’s a complete example that reads from a file, processes the content, and writes the processed data to a new file.
try:
# Reading from a file
with open("input.txt", "r") as infile:
content = infile.read()
# Processing the content (e.g., making it uppercase)
processed_content = content.upper()
# Writing the processed content to a new file
with open("output.txt", "w") as outfile:
outfile.write(processed_content)
print("File processing complete!")
except FileNotFoundError:
print("The input file was not found.")
except IOError:
print("An error occurred with file processing.")
Conclusion
Python’s file I/O functions are powerful yet easy to use, enabling us to interact with files in various formats. With the basics of reading and writing files in hand, you can now manage data storage, configuration files, and more in your Python applications.