File I/O (input/output) is a core skill that everybody should learn. Whether you want to create simple personal scripts or plan on diving into data science, file I/O is a common need that is fortunately easy to implement.
In this article, we’ll introduce how to both read and write files in Python. After introducing the skills, we’ll discuss some design considerations to avoid common pitfalls.
Before we begin reading or writing, the first step is actually opening the file.
To do this, we’ll use the
open() function which creates a file pointer. Our pointer will be stored in a variable. We need to specify in which way we are planning on interacting with the file. There are four modes to interact with a file: read (r), write (w), append (a), and create (x).
We’re going to focus on read, write, and append for the remainder of this guide. So let’s take a look at some sample code:
filename = "app.log"with open(filename, "r") as f:
# <_io.TextIOWrapper name='app.log' mode='r' encoding='cp1252'>
First, we assign our file’s name to a variable called
filename. Then we use the
with keyword in tandem with the
open() function to create a read file-pointer which is referred to as
f within the code block.
Notice how printing
f does not print out the file, but rather the pointer’s information.
The purpose of using
with is to bypass managing the file pointer. There’s nothing wrong with calling
open() by itself, but then you should close the pointer once you are done with the file.
f = open("app.py", "r")
with, the pointer is automatically closed at the end of the block, saving you work.
There are many ways to read in a file. There are two main questions you need to ask before picking a strategy:
- Will I treat this file generically?
- Do I want to read the entire file or just a specific part?
We are going to focus on treating the file generically. Just know that there are libraries designed to better handle specific file types such as JSON and CSV.
There three methods to choose from when generically reading a file.
- read() will return the entire file or up to the number of bytes if specified
- readline() will return a single line or up to the number of bytes if specified
- readlines() will return the entire file or up to the number of bytes if specified, split into a list of lines
If you’re planning on running a single command like a print, then
read() is generally sufficient; however, if you plan to work with the read data then I recommend
readlines() which helps break apart the file.
Unlike reading files, there are two modes available when you want to write to a file. If your goal is to write as if new, then use write (w) mode. This will erase any existing content in your file. If you want to preserve the current contents, then use append (a) mode and your new content will be written to the end of the file.
There are two write methods: write and writelines. Use
write() for a single string and
writelines() for a list of strings — similar to how
readlines() returns a list of lines.
There’s more you can do with files than simple full reads and writes; however, in practice this will carry you very far. Share your comments and experiences below. Thanks for reading!