7.5 Reading and Writing to Files
Sometimes you want to have your cake and eat it too. When it comes to files, that means being able to read and write a file without needing two separate file handles. Well, in Python, this is all very possible. But with great power comes great responsibility, as I will show you in this lesson.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
7.5 Reading and Writing to Files
In the last few lessons we've been talking about reading and writing to files as separate operations, where we create a handle, or a stream, or some sort of pointer to a file with write access. So we can do it with read access, we can write over here, we could read over there or some combination of the two. Well what if we wanted to do those operations to the same file, what if I wanted to read from a file and be able to write to it also, how could I do that? Well if you thought about a logically in essence, you could create two separate handles one with a read access one with write access and try to manage those two things together and it's gonna be incredibly difficult and I don't really advise it. So what's another way to do it? Well, a very effective way that you can do that exact thing within Python is by using what I refer to as +access, so we can use file+access to do both read and write operations using the same stream or the same handle to that file. How do we do that? Let's take a look. Well I've got a new Python script here, and here is the resulting file that we were working with last time when we were writing to our lorem_copy.text. I want to get both read write access to a file so that I can read and write. So how would we do that? Let's start off with what that's going to look like. Let's use our with statement. We are going to open up a lorem_copy.txt and we're gonna save that as lorem copy like that. Now remember this open is going to by default open it with read access. But we want both. So we're gonna use plus access which means you can specify either read or write plus. So I can specify this as write + just like that which means I get both write and read access. I could also do r+ which is going to give us read plus write access. So what's the difference? Why should we have two? Why should a read plus and a write plus? Well, let's walk through an example here and I'll show you why. So we have W plus here let's start off with right plus access which means I can both read and write to this file. So what does that matter. Let's go ahead and read the current contents of this file and then we'll write something to the file which seems like a fairly simple operation. So if we were to print the current contents of the file. So we'll say print Lorem copy dot read, so read the contents of that file to show that we can read it and then we'll go ahead and just write something to it. So we'll say lorem copy dot right, this sure is a great text file something like that something simple. So, now, we've saved that and now what I wanna do, is I wanna go in and I actually want to execute this script. Let's go to our terminal here, and we'll run Python 3. This is going to be file_plus.py and we get nothing. Why do we get nothing? I wanted to read something from this file, I I have the right plus access and I wanted to read the contents were should read the entire contents of the file that should print it out and then we're going to write this sure is a great text file. Well let's see what's in here. Well this sure is a great text file, but everything else has gone on. All right, I'm starting to see this now because we are using the write plus access that means first and foremost it is write access plus read access. So what's the default functionality of specifying write access when we open the file? if the file already exists, it's going to get overwritten, right? So, we had a text file that had some text in it. We're using write plus access, and we're trying to read the file and then write to the file. Well, as soon as we get open access to this with write access, there is nothing in that file anymore it is now been overwritten it is empty. So when I attempt to read it, there's nothing there. And then when I tried to write to it it just overwrites whatever was there with this new content. That is the default functionality of write. And in turn also write plus access so what's the difference with read plus access. Well if I were to come over here to, read plus access. Let's go ahead and save that and we'll come back over here and we'll re-execute this. As you see here, we got, this sure is a great text file. And then if I were to come back over here and take a look. We now have two strings that were put in here, in kind of a, it would seem as though, they were appended together so Is appending the default functionality of read plus access? Well there is a very simple way to find out if that were the case when we were to execute this script, we would be able we would print out two lines. And then we would get a third one right so let's go ahead and try to run this again. So let's run it again and it would look like it is possibly the case. Let's come over here, yes we do get three. Now I do not want you to be confused by thinking that this is necessarily appending. What's actually going on here is the fact that we are doing this read write here. What would happen if I were to slightly modify this script, remove this print and put it down at the bottom will save that. Now think to yourself for a moment, what do you think will happen now? I am going to write first and then read. So at this point we have three copies of this sure is a great file. If it were doing appending then we would think if we were to execute this script again we would have four instances of this string. So let's make sure we have saved this file. Let's go over to our terminal, let's go ahead and write this. Okay well it says this sure is a great text file. This sure is a great text file, let's come back over here and we did not get another one. So that's strange. Why why is that I don't quite understand what's going on. Well what's going on here is very similar to what we were doing when we were talking about navigating a file. Whenever you're navigating a file, whenever you're reading a file, remember we have that pointer that's internal to that file that keeps track of where we are. Remember, the seek and the tell and, if after we read so much, then we we do some other things. Well, what's actually going on here is at the time when we do this right, When we write this, we're starting at the beginning of the file, the pointer always starts at the beginning of the file when you open it. So then it's going to write this to the beginning of the file, which makes sense, because it doesn't look like it's changed because it's the same text. And at that point, the pointer within the file has moved up to this point right here, so then when we do the read? It starts from where the current location is within the file and reads to the end, that is why when we run this script. It only looks like there are two entries in that file when in turn there's actually three because of the location of where we are within the file. To make this a little bit more straight forward and easy to see let's change this right here to say something else Tuts+ is awesome. So now we've changed the text a little bit, we're gonna keep everything else the same and let's see what happens. Let's come back over and let's rerun this and never to get some really crazy stuff now. It looks like it's a little bit different. We got a little bit more text, but what else is going on in here? Now if we were to come back and look at this file, Tuts+ is awesome, and then the remainder of those previous strings. So, as I said, it's all about pointers within the file and where you're currently located. When you're using right plus access you're going to clear out the entire file first. And then started index zero with your writing in your reading but that pointer always advances some number of bytes after each operation. When you use read plus access you do not overwrite the file but you still start at index zero. So if you're gonna write first, it will overwrite the contents at that zero location first. And then if you read a certain amount or write a certain amount or seek or whatever, you're going to move that pointer back and forth depending on whatever operations you're doing. So the read plus and the write plus access are very powerful but it's definitely something that you have to work with and get a little used to. So, you don't start overwriting files and clearing them out, when you didn't necessarily mean to.