Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Data python 3
  • Overview
  • Transcript

3.4 Writing to Files

Time to shift gears a bit and look at writing to files. Although file writing is not overly complicated, there are definitely a few things that you need to look out for. In this lesson I'll show you some common pitfalls.

3.4 Writing to Files

To this point in our journey into the world of files, we've done a lot of looking at how to read the contents of a file in several different ways. Now, it's time to give writing to a file a little bit of love. Now, writing is very similar to reading. You're gonna open a file. You're gonna get a reference to that file. And then, instead of reading you're gonna write to it. So it seems very simple, but there's definitely a few little gotcha's that you need to pay attention to. And I'm gonna show you some of them now. So what I would like to do is I would like to write to a file. So let's create a new script here, and we're gonna save this one as file_write.py, like that. And now I want to go through the process of getting a reference to a file and go ahead in writing to it. But let's be a little bit more interesting about it. I would like to create a copy of the contents that we had in the lower in that text file. So I would need to read that file and I would need to write the contents of that file to another file. So let's kind of work through that together. So the first thing that I wanna do is I want to open lorem.txt and I'm by default going to get access to that as read access. So that's what the default is, and I want to hang on to that as Lorem underscore in. So that's gonna be the in version or actually maybe even in file would be better. So this gonna be the in file that I'm going to be working with the existing file and then I also want to do another with. That is going to open. In this case it's going to be a lorem_copy.txt. Now, as I mentioned before if I don't specify a second parameter to the open function. It's gonna open it with read access. Well, that's not what I want this time. I want to be able to write to this file. So the way that I'm gonna do that, is I want to specify a second parameter with a w. So w means write. So now that I have write access. I need to get this as lorem out file like that and now I have two pointers to two different files. I have the lorem in file in the lorem of out file. So what I would like to do is I would like to read the entire string contents of the in file write it to the out file. So we can do that quite simply by saying lorem underscore out file dot right. And I want to write to it, Lorem in file dot read and that's basically it. So I want to take all the contents of Lorem dot text, and then copy it into Lorem underscore copy dot text. Let's go ahead and save that. We'll flip over to our console here, and we'll go ahead and run this as a Python3 file underscore right from go ahead and run that. And as you saw in the background here in my text editor I got a new file here Lorem underscore copied dot text. And as you can see, I got a one-to-one copy of the contents of my lorem file which is pretty cool. So I've been able to read all of the contents using read, and I was able to write that entire string using write. And that's basically the process of writing to a file is using the write function. So that's cool. Then what if I wanted to write some other content? So maybe we take a break with all this stuff, and you know I just wanted to write some other contents there so let's say with the open again. Let's get that. Let's get that copy file again. Let's say Lorem copy and this is .txt and remember we wanted to use the right access we want to use the W like we did before. Just like that little say that is Lorem, Copy. We'll call it copy this time. Let's just add something else to the file say Lorem underscore copy and let's write. Hello writing to files using Python. So something simple like that. So we'll go ahead and save that. So, let's come back to our console again. Let's execute that again. Everything looks like it worked. Let's check out her file and oh-oh, what just happened? I wrote to that file. Why did it, why is everything going? And that's one of the first caches that you have to pay attention to. When you are writing to files using Python. So what we did here is we opened the file just like we've done several times before. And we opened it for write access and then we wrote to it. Well, one thing that you have to understand that Python is going to do when you open a file with write access If that file does not exist. It will create it for you. If it does exist. It is actually going to erase it or clear it or delete it and recreate it. Overwriting everything you had there before. So if you have important files and you have them backed up, and everything is good and you have multiple copies. You use write access. Then you're gonna be okay, but if you don't and you just overwrote everything that was in that file. You might be in some trouble. So how do we get around this? What if I want to add contents to the file? Not necessarily overwrite the file. Well, there's an additional file access that you can use when opening a file, instead of using Write and that would be A for Append. So if you want to write to a file and clear it out as if it never existed and start over from scratch. Then W or Write is your file access of choice If you would like to append contents to that file then you want to use a for append. So if I use a and I go ahead and save this and come back over and execute my file again, or I execute my script again and take a look at copy. You're gonna see now I have these two lines together. Well, why, why were they sandwiched together here? Well the reason for that is because write function, which is going to write exactly what you specified. And if you are using append it is going to appended to the absolute end of the file. And because we didn't specify any newline characters or anything like that. It's just gonna keep appending it right next to each other all the time. So in order to fix that, if you want to specify spaces in or lines in between there. Then you're going to have to make sure you specify new line characters within your strings. Let's go and save this and run it again. And once we've run it we can come back and take a look at our copy again. And now I not only put the next write statement on a new line because of that newline character. I also specified a newline character at the end of that string As you can see, I have a third blank line here. So, just a couple things to keep in mind when you're writing to a file that file access W means right. It's going to overwrite anything that previously existed, and A is going to append anything to that file. And you do have to make sure you specify new line characters and that sort of structural stuff that you can use to put some spaces in between your lines and kind of format your text to look nice within your file.

Back to the top