From the course: Intermediate Python for Non-Programmers

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Reading a file

Reading a file

- [Instructor] All right, let's move on to talking about how to read an existing file. So in order to do this, you have to have an existing file. I'm going to work with the cheese.txt that we already created. If you don't have that, you can either manually create it or just run your previous Python code that created that cheese.txt and you should be good to go. All right, so with reading, it's similar to when we want to create a file. We're still going to do this open and we're still going to provide the name of the file that we want to work with. In this case, cheese.txt. But here instead of passing an x, or a w, or an a, we're going to pass the letter r and the r stands for read. This means we just want to read what's inside of this file. That's all we're really interested in, okay? So we still put this into a variable called file that we're going to use to work with and I'm going to show you the different ways that we can read a file, and I'm going to start first with the most…

Contents