Hi, welcome to this new video. Welcome to week five. This is our final week of this course. So in week four we ended up talking a little bit about how do you write an image into our disk, right? How do we export an image? Or how do we import images as well? So we're kind of starting to understand a little bit the notion of inputs and outputs. We're going to extend that idea with the idea of reading and writing text. Because many times you might have like a data set or some kind of information and you want to maybe visualize that, or perhaps you want to collect your data or even save the state of a program as a save file, all right? So we're going to start learning how to do that. So let's just kind of understand the notion of inputs and outputs, right. When we read a file, we consider that the input, we bring that into the internal memory of our software. When we write a file, that's our output, right. And we can actually create certain circularity. We could actually start thinking of maybe a save file that we write initially when we want to store, let's say the state of a program, and then eventually we want to run again and maybe load that file, right? You could easily create a save system for your compositions and maybe resume where you left off by using some of these techniques. But we're going to start learning how to do a very simple file. How do we write txt file? There's going to be several file formats that we're going to cover and see how we can actually get into more complex data structures, especially towards the next course. But for now we're going to write a simple txt file. And for that we're going to use the inbuilt function create writer, that it's a kind of a processing function to define a file. In this case a txt file, and how we could actually print line and then use a couple of functions like flush and close to commit to memory and close the program, right? So in the case of the reading of a file, we're going to use a different class, not the create writer, but reader, right. In this we will actually use the input of an external file, such as data txt. This file would need to exist in the folder similar to images, right. We will be using the same folder to access that data, and then we can actually go through each line of that file. And in this case we're going to be using a while statement to just loop through the number of lines that we have available. And eventually just going through all of them. So let's jump into processing and see how we can actually execute this. How do we implement some of these ideas? Okay, so as we save our file, we need to be able to locate the folder in which we save our file. Because this is where anything that we write out is going to be saved. And anything that we read in is going to be obtained from the resources, if you want, that we're going to have available. So let's quickly use an output. We're going to start with writing, and create a writer file data txt. So once you open this line, we could use in this output some functionality, right? I invite you to check out the processing reference to what kind of functionality you have. Sometimes you might not know what kind of functionality is available to me. Always referencing to the reference files is going to be important. So let's say print line, we will say hello, and that's our file. And maybe when we're done we could say output flush. This is a line that is making sure that we are committing that information to an external state, in this case our hard drive output close. And we want to also make sure that we add this line to close the writer so that you don't end up with a file that is still kind of open and thinking that could be in a writing state, right? So let's just execute this line. We haven't drawn anything to the screen, but that's not a problem. We have our text file here, we can open it and we have our hello, right. So you can add more lines such as this one, hello world, right? Again, in this case we're not really using the screen, so we are writing each line. Notice that we are not writing in the same line, but rather different lines. So what happens if I use print? Let's just try that. So the difference between the line print and print LN, ln for line, is that we would actually specify that we move to a next line, right? So if you want to add multiple strings or multiple numbers in one line. Notice that I kind of created a bit of a space here not to have the word hello world written altogether, right. The other thing that we can do, we can put all this in a for loop, right? So for I in range 200, right. We could say, let's just delete the dot for a moment. We can print something like, let's use the STR to convert into a string, the number 50. But you could certainly do a random number here if you want 200 random numbers, right. We execute that. And you can see we have that sequence of number 50 if we do print line. And again, we can do the sequence as different lines, right? So, very useful way of basically writing information into a text file. Again, a text file is a very simple format. I don't tend to use text files too much. You'll see that some other kind of more advanced file formats are going to be a lot more interesting. Things like CSV or JSON files that we're going to learn a bit later. But for now, let's import the random module and create a set of, let's imagine that we're creating a data set of random, I don't know, coordinates or values, something, right? So here we could say let's print the line random rand range between zero and 100. Let's see if we have any problem with that. So we got our file and you should see that all our numbers are in fact random, right? So that's our data, this is our writing. Let's just quickly jump and see how we can read that file now. So let's, for a moment here, I'm going to just leave this aside. So this is our function. We're going to do a multi comment. So this is a way of, kind of, freezing if you want this section of the code. Very useful if you have a lot of lines that you want to kind of suppress for some time being. Let's do a reader I'm going to read the file that we just created, right? That's our reader function. Sorry, the variable line, it's going to be from the reader. We're going to access the read line, I think capital l. There we go. So this line is interesting because this line will advance basically the entity we're currently reading, right? So while reader might contain all the lines that we have available, the read line, it's going to give us our current line, which is going to be the first one, and we can continue doing that. So let's just print to the console the line, right? What are we obtaining here, right? So you see here we have our first value printed down here, right? So that's our first value. Let's imagine that we just copy paste this line once again, right? So nothing has changed. Line, still kind of, we say read line, and we print the next value. You see that the values change, right? So what this shows us is that the read line, basically this line, or the read line function, it's moving the head of the reader towards the next line in the sequence, right? So we basically if we want to read all the lines in the document, we have to create some form of iteration or looping structure. But we don't know how many lines were created. We obviously know because we created the file. There were in fact 200 entries, but we might not know. So let's use a while loop that it's a particularly good loop here. So while the line is not equal to none, meaning that there is no result, we will print the line and also move the line, right. The most important thing is, in fact to move the line. If we don't advance the head, we're going to end up looping forever into this state, because we're going to be reading constantly the first line of the document, right. But with this line, we know that we're moving to the next one. So eventually we'll conclude reading all the entries in the file. Let's see how this works. As you can see here, we can scroll and you can double check with your text file that is saved in the folder. We should be able to read all those files, right? So this is kind of a very simple introduction. How text files can be generated initially with the reader function, sorry, with the writer, and then how we can read those text files or other text files that you might find online with the reader function. And we're going to continue this week looking at more advanced data types and see how we can actually create more interesting files. So I'll see you in the next video