From the course: Python: Working with Files

Open files in Python

- [Instructor] Before accessing the contents of a file in Python, the file must be opened. A file can be opened in a few different modes and the mode defines how its contents can be manipulated and accessed. r mode opens the file in a read only mode. In this mode, the files contents can't be changed, but they can be viewed. If the file does not exist, in error is returned. We also have the a mode for appending. You can use this mode to append onto the contents of a file. w mode opens the file for writing. If the file does not exist, Python automatically creates the file. By default, all files are read from and written to via text mode, but you can specify binary mode to read and write using binary. To specify binary, you can add the plus sign with a b. There is also one less common mode called x mode that creates the file if it does not exist. If it does exist, an error will be returned. Let's open a file using Python. Open is a built-in Python function that's a part of the Python standard library. So we don't need to import anything here. We'll create a new function called print content. Inside the function, we'll open up a text file in read mode. We'll use the open function and specify the file path and the mode. Read mode. You can find this descriptions folder and its associated file in the exercise files of this course. For this example, the Python script and the descriptions folder, are on my desktop. Back to our script, we'll save the open file in a variable called F so we can run functions on it and interact with the files contents. Let's read in all the contents of the file. We'll write f.read and see if the output in a variable called contents. Let's display this to the console. Then we can close the file with f.close. While most of the time you only have to close a file if you're writing to it, it's always a good practice to close any files you open. This allows Python to manage the program's memory in an optimal way. Let's call this function and see what it outputs. To run the program, I'm in my desktop and our script is script.py. In the output, we see the entire contents of the file. There are lots of different ways you can read in a file and we'll explore them in later videos, but let's try another mode. Let's try writing to a file. We'll use the same file, but we'll use write mode instead, or w mode. Then we'll write a new description. After writing the description, we'll close the file. This will overwrite the current contents of the file. The old description will disappear and be replaced by this description. Let's call the function. In the output, we'll see a new description because we call the write function before the read function. We overwrite the files contents before displaying them to the console. Let's run it. And there's our new description. In our desktop, If we click into the file, we'll also see that the content has changed. This isn't just something that happened in Python, it affected our files. Before we move on, we can refactor our code to make it a bit clearer. Using the width keyword. It makes it easier to quickly open a file, manipulate its contents, and automatically close it. It can be easy to forget to close the file when working with an unmanaged stream or resource. So using the width pattern, is a great way to get around that.

Contents