Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Exploring the Different File Modes

00:00 Previously, you briefly touched on opening files in Python using either text or binary mode. In this short lesson, you’ll take a closer look at text mode and learn about even more specific constraints you can impose on text files when you open them in Python.

00:18 Recall that Python opens the specified file in text mode for reading by default. You can change this by supplying one of the valid letter codes through the mode argument of either the open() function or the Path.open() method.

00:32 Also, keep in mind that text mode is implied, so you don’t have to explicitly pass the letter t, although you can if you really want to. From now on, you’ll assume that all letter codes have that implicit t sitting there.

00:49 Okay, here are the letter codes representing various modes for reading and writing text files in Python. The default mode is the read-only one indicated by the letter code r, which means that your file must already exist, or else you’ll get an error when you try to open it. When the file gets opened, it’ll be positioned at the beginning, allowing you to read its contents in order.

01:11 Because this is a read-only mode, you won’t be able to write any new content to the file.

01:18 When you use the r+ letter code, you’ll be able to read as well as write new content to your file. While the file gets opened at the beginning, you can freely move forward or backward within the file before deciding whether to overwrite a part of it. Just as before, the file must already exist. In the write-only mode, the file may exist or it may not, in which case Python will create one for you.

01:43 However, when the file does exist, it’ll be truncated, meaning its content will be completely wiped out, so be extra careful when opening files in this mode.

01:53 That’s also why opening in write-only mode always puts you at the beginning of the file. If you want to read and write at the same time, then you can use either the r+ mode, which you’ve already seen, or the w+ mode. Both are very similar, but w+ mode will either create or overwrite your file. So again, be careful when using this mode.

02:16 The next mode, denoted with a letter a, stands for append-only. As the name implies, it’ll let you add new content at the end of the file, which is where it gets opened. Unlike in the write-only mode, you can’t freely move around within the file. Also, the file may not exist, but when it does, then the append-only mode won’t overwrite any existing content, ensuring its integrity. If you’d like to append and read the file at the same time, then use the a+ letter code, which will let you move through the file and read its parts, but without allowing you to overwrite anything. Finally, the least common file mode is the exclusive create, which is denoted with a letter x.

03:00 This mode won’t let you open an existing file, and instead it’ll create a new file for you and open it in write-only mode. On the other hand, if the file already exists, then you’ll get an error.

03:13 The choice of the right file mode is essential when working with files in Python because it’ll determine the set of operations available to you.

03:23 In the next lesson, you’ll combine everything you’ve learned so far by practicing reading and writing data from and to text files in Python.

michael on Feb. 3, 2024

Hi Bartosz,

many thanks for the well structured and informative lesson.

Just one question concerning the r+ row: Is the “x” in last column (Overwrite File) correct?

Cheers

Michael

Bartosz Zaczyński RP Team on Feb. 4, 2024

@michael I’m glad you found this lesson useful, and I appreciate your attention to detail 😊

Yes, the “x” in the last column for the r+ row is indeed correct. When you open a file for updating, i.e., reading and writing, then Python won’t truncate your file’s content. Otherwise, you wouldn’t be able to read what’s already been written in your file!

michael on Feb. 4, 2024

Many thanks!

michael on Feb. 4, 2024

Hi Bartosz,

one more question, just for clarification. The entries in the last column of row three and four mean truncation (or deleting the content of the file).

Otherwise I do not fully understand the difference between r+ and w+.

Cheers

Michael

Bartosz Zaczyński RP Team on Feb. 5, 2024

@michael The r+ mode can be useful when you want to open an existing file for reading and writing since it won’t truncate your file. At the same time, this mode won’t create an empty file for you if one doesn’t already exist.

In contrast, the w+ mode will always truncate the file or create an empty file when it’s missing. This is different, though, than the a+ mode, which only creates the file when needed but otherwise doesn’t touch an existing file’s content.

michael on Feb. 5, 2024

Thanks for clarification. By the way, I find the table with the different modes. But it’s not easy to convey all the information.

michael on Feb. 5, 2024

Some words were missing :-).

By the way, I find the table with the different modes very useful.

Become a Member to join the conversation.