 In this tutorial we're going to be referencing our external text file of lines. What is that you may ask? Boom! On the screen right now we have got a text file with a list of lines that we want to read into Python. Currently we've got this list of lines hard-coded and we need to go ahead and change that. We'll be revisiting some more for loops and we're going to be checking out how to open and close files properly in Python. Okay, a lot to get through, let's get stuck into it. Hey crew, it's The Serving Scratchy here, teacher-serfer programmer and on this channel I help curious learners just like yourself along on your learning journeys. Welcome back to out from Scratch the Python series where we're here to get you nice and familiar with a text-based programming language called Python. We coded a game in Scratch a spelling game and now we're trying to replicate it in Python and we're nearly done. Okay the first thing that we need to do is create a text file with all these lines. Now you might already have this text file on hand and if you do fantastic go ahead and click these three little dots and press upload file navigate to where it is and click open and you'll see when I click on it it opens up to that file and we've got all those lines listed in it. You may not have these and if you don't hopefully you have already gone ahead and used our starter project you can go ahead and copy all of these lines here head over to your file and paste them in. I'm just going to sort out the formatting here pressing shift tab to remove that indentation level. There's two things that are the problem here we've got some double quotes and we've got some commas here. So let's go ahead and remove these double quotes and commas. If I press command F or control F on windows I can search for the double quote comma and I can press this little drop down and we can replace it with nothing. So let's go ahead and do that we've just removed all of those and now I want to search for a double quote and I'm going to just replace all of those and that's how you can strip those files or those double quotes and comma from this particular file. Now let's jump back over to our python project by clicking the main dot pi in our pane here and we can close our files sweep. Let's scroll back down to where we've been working just beneath the extract words and sentences. Let's create a new function definition and we're going to call this read lines from file. We're going to give this one a file name and we can use our default parameter value here and we're going to use the file name that is in our file. So I just told you to close that I'll just open it back up there just to see exactly what it is and we'll make it lines dot text. So that way we don't have to supply this function with anything it has this default value of lines dot text. Let's put our colon at the end of the line get down to business. Okay so if we want to open a file in python we can use this syntax so creating a new local variable here called file and we can use a built-in function called open and we can just feed into open that file name. Okay and just to show you that in action down here in the console I've just done that exact line of code and now I'm just going to call another function called read on that file. So we've just opened the file lines dot text now I'm going to call read and you can see we've got all that text content from that file printed on the screen. So you can see we've just done file dot read now whenever we finish using a file we need to close it so file dot close and this is the basic idea of what we're doing here but there are some other functions that we're going to be using in place of these and just like on your file system when you're referring to links or file names this works because it's in the same directory as the python file if these were in two different directories then this would break down. You can get a URL and open up a file that's on the net you just get a direct link to it you just need to make sure that you have an internet connection to do that. Okay let's check out another way to open up a file I know I'm probably going to forget to close it and there's some syntax that we can use to bypass this need. The way to do that was we use the with syntax and we can go with open we're going to give it that same file name and we're going to open it as file so we're just doing the exact same thing here but with different syntax we're still creating a local variable here we're opening it and I've got to put my colon on the end of it the only difference here is that we're going to do all of our work inside of here and we no longer need to call close so this is just a better way to be opening files also it has some built-in error handling in case we can't locate that file I'll leave a link down below in the description for you to go and suss out the with syntax. Notice that we're working on an indented line within this with block so before we did file.read and that read the whole data but we don't want to do that we want to read each individual line we can use a for loop to help us out with that so we can go for line in file.read lines and what read lines does is it automatically breaks up all of those lines basically into a list we need to put a colon character at the end of our for loop here and then we need to do something with this line what we're going to do is we're going to create a new variable called lines and it's going to be a list and we're going to create that outside of the with block here so then back down inside of our for loop we can start to do some work on lines and we're going to add something into it we're going to append remember we use that in the last tutorial thing that we're going to append is we're going to be working on that line and we're going to be splitting the line based on that colon delimiter remember and we only want to do it the first time so we've actually done this line of code before in our split line function above we'll just go to that you can see that here we've got out the same syntax of line.split with the colon and what this is going to do is create a list of tuples containing both our word and our sentence and once we've done that we can unpack or even unzip that list of tuples into our words and sentences list okay so that was a lot of talking let's get visual and see what all this is going to look like so let's just sort of test that where we are we're going to return the value of line so we can just see exactly what we're getting here and I also want to show you what happens with read lines so I'm just going to press the run button here what I'm going to do is just create a new file and we're going to open it here we're going to feed it line.txt now if we go file.readlines I just want to show you what this line of code does you can see it's a little bit hard to read so what I'll do is I'll clear that and I'm going to put that inside of a pretty print and if we scroll up you can see that we have split all of our lines here into a list now if I try and call read lines again you'll see that we get an empty list this actually only works once now if I go ahead and call read lines from file we'll do the extra business of the follow up here and we'll see what the return value is okay I don't need to pass it a file name because we're already giving it lines.txt as a default value let's click enter and you can see we've got that list again and now I'm just going to put that in a pretty printed version let's blow it up so you can see what's going on here and you can see that we've got a list of lists here so it didn't actually give us a tuple but it's given us the first list item of like a nested list so the first item is a list of dash and the sentence so what we can do is iterate over this and unpack each of these values into the words and sentences like what we've done up here but since you've already done it I'm going to show you how to do it in a another one liner so we don't actually need to repeat that follow by that the function that we're going to be using is called zip and I'll put a link down in the description for you to read it up a little bit more about it but essentially what zip does is it takes like two iterables so let's say like list one and list two and it combines them into a tuple value so this would turn into an iterable with some tuple value so you take the first value of list one and the first value of list two and it would be inside of one tuple okay and it would just keep adding those tuples for as many items as you have in these lists here but we're starting with the reverse because we actually have a list of tuples here with a word and a sentence and we've got lots of these we could just keep adding words and sentences so what we want to do is unzip this into the word and sentence so I'll show you how we go about do that we're going to use our multiple assignment again we're going to create words we're going to create sentences here and then we're going to call the zip function the way to unzip is using a special syntax of using an asterisk character here and we're just going to pass in those lines that we've done and that's it this will unzip those words and sentences and assign them to these variables here then we can just return those values instead of lines all right let's click the run button let's call read lines from file and you can see we've got the same output I'll expand it up let's clear that and then we'll put it inside a nice pretty print and you can see here it's given us a tuple of tuples or nested tuples where the first tuple is all the words there so this is the words tuple and then we've also got the sentences tuple here just going to reduce the size of this so we can scroll back up there okay so that's going to bring this tutorial up to an end and I hope you noticed that we basically did all five tutorials that we've done up in this series up until now here all in one function and that is the power of python and text-based programming it is so simple to do what seems like a lot of work to do in a scratch but the whole point of going through and building out all of these other functions is really to build up your familiarity with python to build up your understanding of functions and parameters and variables and all those sorts of things in the next tutorial we're going to start the game component of our python game here and over here in scratch that's where we use dot and we've got lots of game functionality over here in scratch that we need to implement so we've got this say word say sentence ask the user to spell the word and then we need to check the result and give some feedback so we're going to rapid fire all that in the next tutorial there's about three left to go can't wait to catch you in it but until then I'm off to go find a wave I'll see you in the next one