You already know how to read files. Now, let's talk about how to write files. For the most part, it's pretty straightforward and not going to be that surprising but there are a few differences that I want you to be aware of. So, let's take a look. When you want to write a file, you have to first open it. So that shouldn't be a surprise. This is no different than when we're reading a file, but I have to pass a different mode string to the Python open function. Here, we have some more choices, okay? So, the common one would be "wt", meaning write a text file. I can write binary files as well, just like we could read them, but we're going to focus on text files. In this class, we'll continue to do that. But w here, what that does, if the file doesn't exist, it creates it. If it does already exist, it erases it first. So, if you open a file in mode w, whatever was there, if there was something, is now gone. If you don't want that, instead you want to write to the end of the file, you can use a. That appends to the end, so all writes will happen after it's already there. It doesn't erase the existing contents. And I do want you to notice that you can add a plus sign in here if you want to open a file to both read and write that file. That's a little tricky to make sure that you do right and it involves a couple of other things in changing where you are so that you're not reading and writing exactly the same thing at the same time. But let's ignore that for the moment and go back to just opening the file and let's print it and see what happens here. Okay. So, we remember when we have an open file, it's some TextIOWrapper. You can see that the mode here is "wt", so this file is open for writing, and when we're done, we want to make sure we close it. Always close your files. All right. That's nice, but let's actually write the file now. So, you notice here that I defined a function called checkfile that takes a file name, it opens up that file, and reads the contents and prints them out. That's going to allow us to confirm that we actually wrote something to the file. Now down here, you can see that I'm actually trying to write the file. So first, I need to open it, and I'm going to open it with mode "wt", which is going to erase the file and allow me to write to it and it's going to assume that it's a text file. There are several different ways that we can write to a file, but let's look at two of them. One is that an open file, you can call writelines. It has a writelines method that takes a list of strings, and so you can see here I have a list of two strings, those are going to be the two lines that I'm going to write to the file. Or, I can just call a function called write that just takes a single string and writes an entire string out to the file. And when I'm done, well, I better call close. And note that writelines could have as many lines as you want and it's not really actually interpreting those strings as lines, you have to add the newline at the end of them. And write can take any string that could be as long as you'd like. You'd notice that I actually have two lines contained in that string here. So let's actually run this and see what happens. So, you can see that checkfile printed out a four-line file, which is what I expected, writelines, I wrote two lines, and in write, I wrote two more. So, that all looks good. Now let's see what's going to happen if we write the file again. So, I'm going to open the file again with mode "wt", and remember, mode w says erase the file first. Now, you're going to have to take my word for it. It didn't actually exist the first time we ran it here. But, let's see what happens now when I try to write it twice. You can see that still the first time, we got the initial file contents of the four lines that I wrote. I opened it a second time and get the open file object outputfile2 and write the string overwriting contents into the file and close it. And when I read back what's in the file, you can see none of the original contents are there. Just now, it just says overwriting contents. Okay. What if that's not what I wanted? What if I actually wanted to append it to the end of the file? Let's see what happens here. I'm going to open it yet again. This time, I'm going to use mode "at"and I'm going to append to the end of the file because I'm using mode a and so I would expect that I would see overwriting contents and then appending to contents appearing in the file, and let's see if that's the case. Yes, in fact it is. All right. So, you need to be careful and think about whether or not you want to erase the contents when you're writing to a file or append to the end of an existing file. So, hopefully you agree that writing the files is actually pretty similar to reading from them, right? We need to open them and make sure we use the proper mode string and then we can use the write method or the writelines method to actually put contents into the file. The big difference here is we've got to decide, do I want to overwrite what is already there, if the file exists, or do I want to append to the end of the file if it exists? Other than that, hopefully you're not going to struggle too much writing files.