Python Read File – How to Open, Read, and Write to Files in Python
Files are essential for storing persistent data outside your running Python programs. Whether it‘s reading configuration settings, saving output data, or parsing large datasets, knowing how to properly work with files is a critical skill for any Python developer.
In this comprehensive guide, we‘ll dive deep into how to interact with files in Python. You‘ll learn all the basics of reading and writing files and some more advanced techniques. Let‘s get started!
File Paths and the open() Function
Before you can start reading or writing to a file, you need to obtain a file handle by opening the file. This is done using the built-in open() function which takes a file path as an argument.
Here‘s a simple example of opening a file for reading:
f = open(‘example.txt‘, ‘r‘)
The first argument to open() is a string representing the path to the file you want to open. This can be an absolute path or relative to your current working directory.
The second argument is the mode, which specifies whether you want to read (‘r‘), write (‘w‘) or append (‘a‘) to the file. You can also use binary mode by adding ‘b‘ to the mode string. If omitted, the mode defaults to ‘r‘ for reading in text mode.
It‘s important to always close a file after you‘re done using it to free up resources:
f.close()
Forgetting to close files is a common source of bugs, which is why it‘s recommended to use the with keyword instead. This automatically takes care of closing the file once the block completes:
with open(‘example.txt‘, ‘r‘) as f:
# Perform operations on the file
Reading Files
Once you have a file handle opened for reading, there are a few different ways to read data from it. If you want to read the entire contents of the file into a string, you can use the read() method:
with open(‘example.txt‘, ‘r‘) as f:
contents = f.read()
print(contents)
For reading line by line, you can treat the file handle as an iterator and loop over it:
with open(‘example.txt‘, ‘r‘) as f:
for line in f:
print(line)
This is memory efficient for large files since it only reads one line at a time into memory. You can also use the readline() method to read a single line or readlines() to get a list of all lines.
Writing to Files
To write to a file, you open it in write (‘w‘) or append (‘a‘) mode:
with open(‘example.txt‘, ‘w‘) as f:
f.write(‘Hello world!\n‘)
f.write(‘This will overwrite the file.\n‘)
Opening in write mode will overwrite any existing content. To add to an existing file, use append mode instead:
with open(‘example.txt‘, ‘a‘) as f:
f.write(‘This will be appended.\n‘)
After writing, the file will automatically be closed at the end of the with block. If you forget to close it yourself, Python will do it for you, but it‘s still a good habit to be explicit about it with close().
Handling Exceptions
Whenever you work with files (or any kind of I/O) it‘s important to handle potential exceptions that could occur. The most common ones when dealing with files are FileNotFoundError when trying to open a file that doesn‘t exist and PermissionError if you don‘t have permission to access the file.
Here‘s an example of catching exceptions when opening a file:
try:
with open(‘nonexistent.txt‘, ‘r‘) as f:
print(f.read())
except FileNotFoundError:
print(‘The specified file was not found!‘)
except PermissionError:
print(‘You do not have permission to open this file!‘)
By using a try/except block, you can gracefully handle the error conditions and display a helpful error message instead of crashing the program with a stack trace.
Binary Files
So far we‘ve only looked at examples of working with text files. For reading and writing binary data like images, you need to open the file in binary mode by including ‘b‘ in the mode string.
Here‘s an example of copying an image file:
with open(‘example.png‘, ‘rb‘) as input_file:
with open(‘example_copy.png‘, ‘wb‘) as output_file:
output_file.write(input_file.read())
We open the source file for reading in binary mode (‘rb‘) and then create a destination file in write binary mode (‘wb‘). We then use read() and write() to copy over the file contents.
Seeking Positions
When reading or writing to a file, your position starts at the beginning of the file and advances as you go. Sometimes you may want to change this position explicitly to a different place in the file. This is known as seeking.
To seek to a specific byte position, use the seek() method on the file handle:
with open(‘example.txt‘, ‘r‘) as f:
f.seek(10)
print(f.read())
This example will skip the first 10 bytes of the file before starting to read. You can also call seek() with a second argument to change how the position is interpreted:
- 0: Absolute position from start of file (default)
- 1: Relative position from current location
- 2: Offset from end of file
f.seek(10) # Go to absolute position 10
f.seek(5, 1) # Go forward 5 bytes from current position
f.seek(-5, 2) # Go backward 5 bytes from end of file
The tell() method can be used to find your current position within the file:
print(f.tell()) # Print current byte position
File-like Objects
In addition to actual files on disk, you can also use open() and the file handle interface on other types of objects that act like files. These are known as "file-like objects".
A couple of common examples are sys.stdin, sys.stdout and sys.stderr which represent the standard input, output and error streams.
Here‘s an example of reading from stdin:
import sys
for line in sys.stdin:
print(f‘Read: {line}‘, end=‘‘)
This will continuously read lines from standard input until it reaches the end. You can pipe output from other programs into it or type directly into the console when running it interactively.
Many libraries also have APIs that return file-like objects. For example, the urllib.request.urlopen() function retrieves the contents of a URL as a file-like object:
from urllib.request import urlopen
with urlopen(‘http://example.com/‘) as url_file:
print(url_file.read().decode())
The pathlib Module
For more advanced file and path operations, Python 3.4 introduced the pathlib module. This provides an object-oriented approach to working with files and directories.
To open a file using pathlib:
from pathlib import Path
p = Path(‘example.txt‘)
with p.open(‘r‘) as f:
print(f.read())
The Path object has many useful methods for manipulating file paths like absolute(), rename(), replace() and more. Check out the official docs for details.
Conclusion
You should now have a solid understanding of how to work with files in Python. We‘ve covered all the key topics like:
- Opening files with the open() function
- Reading and writing data in text and binary mode
- Using with blocks to automatically close files
- Handling exceptions
- Seeking to different positions within a file
- Using file-like objects
- More advanced options with the pathlib module
There‘s still more to learn to fully master file handling in Python, but this guide should give you a strong foundation to build upon. The official Python documentation is always a great resource for diving deeper.
I‘d love to hear how you‘re using Python to work with files in your own projects. Reach out on Twitter at @YourName to share your experiences!