Files - txt

We will learn how to open, close, and update text in a text file. Following lessons will cover additional file types.

The text files we will look at end with a .txt extension. You have probably seen these before. The ending of a file helps a computer know what type of file it is. For example, a file ending in .jpg is a picture file. A file ending in .mp3 is an audio file. A file ending in .py is a Python file. A file ending in .txt is a text file.

There are some packages to help import some special filetypes, but for text files we may use a built-in function - open.

In the above code block, the variable fileA stores the contents of the file. We can then iterate through it or search through it if that's what we want to do. In this example, the filename is syrup_flavors.txt. We pass the file name in as a string into the open function. Additionally, the "./" indicates this file is in the same directory as the code that is calling it. If we wanted to open a file in a different directory, perhaps called syrups, then we need to include it in the file name: open("./syrups/syrup_flavors.txt"). We will cover opening files in much different locations later when we learn imports.

We probably want to do something with the contents of the file. Well we can iterate through it, much in the same way we learned iteration in the context of loops. There are a couple ways we can do this.

For reference here's the contents of the file "syrup_flavors.txt"

Let's look at a few ways we can iterate through the file.

With the method read

With the method readlines

With no method

Let's go over these 3 ways. When we iterate using the method read, we look at one character at a time. This is usually not very helpful. We can iterate line by line with the method readlines. This is common enough that Python defaults to this when iterating through a file as in the third example. Note in the last two that print adds a new line at the end of the passage. However in the file there are already new lines (maybe not visible when reading the file, but they're there as a special character \n). So to avoid this we can use a built in string function called strip to remove leading and trailing white space and new line characters. Adding this would look like the following.

Closing a file

Now that we're done with the file, good practice is to close it using the close method. When we're done using a file, calling file_name.close() ensures that all of the changes we made are saved properly and frees up system resources that were being used to keep the file open. It's like turning off a light when leaving a room; it saves energy and is good practice.

Opening with permissions

When we open a file, we can open it with different permissions. If we want to read it, then we can use the argument "r", this is the default permission. If we want to write to a file, use "w". If we want to write to a file, but only at the end (keeping any contents thay may exist in the file already), then use "a". If we want to read and write to a file, then we can combine permissions with "r+" or "w+". The former will keep the file intact until some writing occurs and the latter will first clear the file so that it's empty. These two ways of opening a file are less used as controlling what is read or written becomes hard to keep track of.

Here's some examples of using "w" and "a" to open a file.

After the first close the contents of the file becomes

And after the second close, the contents of the file becomes

Opening with with

We can also create a context manager using the reserved word with for opening a file. The advantage of this is that we don't need to worry about closing the file. The file will automatically close when the code block is finished.

Practice Question

What will the code below do?

score: 0%


Let's learn other file types we can open, such as CSV's in the next lesson!