Can you read and write from a file in python?
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Reading and Writing Files in Python Show
One of the most common tasks that you can do with Python is reading and writing files. Whether it’s writing to a simple text file, reading a complicated server log, or even analyzing raw byte data, all of these situations require reading or writing a file. In this tutorial, you’ll learn:
This tutorial is mainly for beginner to intermediate Pythonistas, but there are some tips in here that more advanced programmers may appreciate as well. What Is a File?Before we can go into how to work with files in Python, it’s important to understand what exactly a file is and how modern operating systems handle some of their aspects. At its core, a file is a contiguous set of bytes used to store data. This
data is organized in a specific format and can be anything as simple as a text file or as complicated as a program executable. In the end, these byte files are then translated into binary Files on most modern file systems are composed of three main parts:
What this data represents depends on the format specification used, which is typically represented by an extension.
For example, a file that has an extension of File PathsWhen you access a file on an operating system, a file path is required. The file path is a string that represents the location of a file. It’s broken up into three major parts:
Here’s a quick example. Let’s say you have a file located within a file structure like this:
Let’s say you wanted to access the Now let’s say that your current location or current working directory (cwd) is in the
But
what about
The double-dot ( Line EndingsOne problem often encountered when working with file data is the representation of a new line or line ending. The line ending has its roots from back in the Morse Code era, when a specific pro-sign was used to communicate the end of a transmission or the end of a line. Later, this was standardized for teleprinters by both the International Organization for Standardization (ISO) and the American Standards Association (ASA). ASA standard states that line
endings should use the sequence of the Carriage Return ( Windows uses the
This same output will be interpreted on a Unix device differently:
This can make iterating over each line problematic, and you may need to account for situations like this. Character EncodingsAnother common problem that you may face is the encoding of the byte data. An encoding is a translation from byte data to human readable characters. This is typically done by assigning a numerical value to represent a character. The two most common encodings are the ASCII and UNICODE Formats. ASCII can only store 128 characters, while Unicode can contain up to 1,114,112 characters. ASCII is actually a subset of Unicode (UTF-8), meaning that ASCII and Unicode share the same numerical to character values. It’s important to note that parsing a file with the incorrect character encoding can lead to failures or misrepresentation of the character. For example, if a file was created using the UTF-8 encoding, and you try to parse it using the ASCII encoding, if there is a character that is outside of those 128 values, then an error will be thrown. Opening and Closing a File in PythonWhen you want to work with a file, the first thing to do is to open it. This is done by invoking the
After you open a file, the next thing to learn is how to close it. It’s important to remember that it’s your responsibility to close the file. In most cases, upon termination of an application or script, a file will be closed eventually. However, there is no guarantee when exactly that will happen. This can lead to unwanted behavior including resource leaks. It’s also a best practice within Python (Pythonic) to make sure that your code behaves in a way that is well defined and reduces any unwanted behavior. When you’re manipulating a file, there are two ways that you can use to ensure that a file is closed properly, even when encountering an error. The first way to close a file is to use the
If you’re unfamiliar with what the The second way to close a file is to use the
The Most likely, you’ll also want to use the second positional argument,
Other options for modes are fully documented online, but the most commonly used ones are the following:
Let’s go back and talk a little about file objects. A file object is:
There are three different categories of file objects:
Each
of these file types are defined in the Text File TypesA text file is the most common file that you’ll encounter. Here are some examples of how these files are opened:
With these types of files, >>>
This is the default file object returned by Buffered Binary File TypesA buffered binary file type is used for reading and writing binary files. Here are some examples of how these files are opened:
With these types of files, >>>
Raw File TypesA raw file type is:
It is therefore not typically used. Here’s an example of how these files are opened:
With these types of files, >>>
Reading and Writing Opened FilesOnce you’ve opened up a file, you’ll want to read or write to the file. First off, let’s cover reading a file. There are multiple methods that can be called on a file object to help you out:
Using the same >>>
Here’s an example of how to read 5 bytes of a line each time using the Python >>>
Here’s
an example of how to read the entire file as a list using the Python >>>
The above example can also be done by using >>>
Iterating Over Each Line in the FileA common thing to do while reading a file is to iterate over each line. Here’s an example of how to use the Python >>>
Another way you could iterate over each line in the file is to use the Python >>>
However, the above examples can be further simplified by iterating over the file object itself: >>>
This final approach is more Pythonic and can be quicker and more memory efficient. Therefore, it is suggested you use this instead. Now let’s dive into writing files. As with reading files, file objects have multiple methods that are useful for writing to a file:
Here’s a quick example of using
Working With BytesSometimes, you may need to work with files using byte strings. This is done by adding the >>>
Opening a text file using the You can actually open that file in Python and examine the contents! Since the
Sure enough, when you open the file and read these bytes individually, you can see that this is indeed a >>>
A Full Example: dos2unix.pyLet’s bring this whole thing home
and look at a full example of how to read and write to a file. The following is a This tool is broken up into three major sections. The first is
Tips and TricksNow that you’ve mastered the basics of reading and writing files, here are some tips and tricks to help you grow your skills.
|