 So far we've been talking about only reading files, but what happens when I want to write files? Maybe for example, I want to take that readings that txt file that I'm working off of and I want to extract some data And then maybe instead of just displaying it to my computer or displaying it to the screen I would like to store that information This typically is through something that we like to call preprocessing. So I have reading a file I do some processing to create another file that may later on be used just in future data analysis So let's see. There we are. So for example, I've had that readings file. I'm reading it. I read the contents I am gonna just move things around just a little bit just to Have things a little more together. So for example, I'm going to put the contents Together in one area and I'm actually just going to Close the file as soon as I'm finished reading it. This is typically how I like to write files that involve File handling I'm done with that file now I you know it all of its contents have been read into memory stored in memory They are now a text, you know a string in my computer program I no longer need the file itself and the reason for this is because You know when we're dealing with files If you have it open Your operating system doesn't allow it to be used or manipulated elsewhere That's just it's kind of rule of thumb because it doesn't want you to try to read something But you're doing something to it and you may have sort of a an issue on do I I read in the thing That you just edited or not so as a good rule of thumb As soon as you do something like read lines Dot close immediately is the way I would recommend it if you're doing a read line instead In that case right afterwards just like I had with the after the for loop So again, the idea is I want to take These numbers that I'm getting here and my sake maybe I want to do some type of analysis I want to do just some simple processing. I'm going to take each one of these numbers divide them by 10 because they're reading at a hundred, you know, it's not that it was 750 degrees in this kitchen It was 75 degrees the thermostat did weird things So in that case, well, I want to convert that into Just I want to make that smaller and then I want to write those to their own separate file So in that case, I'm going to make another file Typically, I like to call this something like output Phi or Phi output just Noting the difference between it being my input file and my output file. So in this case Converted so the same kind of principles come into play here. You notice. I'm using the relative file reference Again, I go up a directory. I see the data directory go down to it And in this case, I'm going to now create a converted that txt file So the same kind of concepts come in here and now that Phi is no longer being used I am going to use its Variable name again. That just is my sort of rule of thumb. I could have been using file name, but that's you know Different choices going on there But again, the idea is now instead of having an R for read I'm going to have a W for write And so the same kind of concept comes into play here I'm reading in in particular all these different lines now in this case. Actually, I don't need this anymore I'm not printing those files. I could but I'm not gonna Same kind of concept that has gone on I have my Phi dot open. I'm going to be working off of this Now for the content, I'm still traversing them. I'm still splitting them. I'm going to instead of just print my temperature I'm gonna call it temp equals Reading three now again, it is just a string So I am going to convert it into an integer and for our sake just for again What I want to do with just this example divided by ten so take my string of three numbers In this case three numbers. Hopefully it never hit a hundred degrees in the kitchen But take these three numbers take these numbers that are currently a string convert them into the integer divide them by ten Now like I said, I want to take this and I want to write it to another file, right and With that in mind I can come in and I again have that Phi file Phi Again this version not my readings. I'm reusing the variable name dot write Now you see I have two options write and write lines If we take a look again, right is specific to writing to one line And you'll see this in a second or multiple lines I could make a direct make a list add every number to that list and then say write the lines And it'll write it on its own lines To each their own really I'll show both variations But just to kind of show this right Tim Same kind of concept goes on here now that I'm finished I you know, I'm going to traverse every single number now that I've traversed every single number It's a good to close it now notice. I'm doing that outside of the for loop again This is the idea is do it the entire time if I closed it right away you get an error and for that sake Let's just see what error you get Right must be string now float. Oh, okay. I know that so I got a few different things going on here So it doesn't like the fact that I'm attempting to write a Also what I'm looking for it doesn't like that I'm attempting to write an integer to this so there are a few different options I could do I could come in here and convert this back into a string and Now it freaked out. There's another error for other things going on there One of the things I typically like to do for this type of situation is I like to create something called line line and In particular for this, you know again We've seen me use line a few times in class the entire idea here is what this is doing is this is serving as sort of a template string that I'll then use dot format To manipulate so in my case I will say I'm gonna add some words reading and just reading with my brackets first one and then inside of my right line dot format Temp now again, I also have an error of IO operation It you can't see the a little bit that saying on a closed file again That's because I attempt I'm attempting to write like The second temperature on a file that's currently closed Because the next thing I did was close after I wrote that first line That's why like I was saying you want this to be after the for loop So now if I run this Okay, no errors. You can see not a single error going on there There we are not a single error going on here and if we take a look inside of that directory Again, okay, there's data. There's my converted txt's and if we look inside there You can see reading 76.0 now you are noticing. Yes. Oh in fact, it's just reading everything Attached to each other and that's why for example in the slides I also showed that slash in if I did want to make sure that when I'm doing dot writes Throwing a slash in at the very end is going to say write this line and Hit the inner key, you know, go ahead and move to the next line before you add in anymore. So in this case I've rewritten it this time And as you can see, you know, no pad plus plus is saying, oh, you know, it got updated This is what it got updated to so in this case. Oh look at everything's on its own nice little line So the same kind of concept can come in like I said, there was a dot write as well that we could Work off of and so I'm gonna say To write is gonna be it's nice little square brackets instead of running the file dot write Again, we're going to say to writes is Gonna get all these new files added in or all these lines added in I don't I'll keep the slouch in for now just For your sake think about what might happen and then phi dot write lines to write Okay, I run this Okay, again, no errors going on there. I reload it And as you can see it's going to go ahead and add in all those lines Now I don't believe the slash in I believe I can Remove that slash in in this case and it will do the same thing. Oh no, okay, so even with the Write lines just as a fun little thing. You do need the slash in as well So with that That's how you can start to write to a file