 Okay, today we're going to play with Python a little bit and we're going to be reading text files But we're going to be reading certain lines that we tell the code to read So let's make our text file real quick here. I'll call it file dot txt I'm going to use vim to create it and I'm going to say line line 1 line 2 line 3 line 4 and line 5 I'm going to save that And at this point I am going to start creating our Our Python script, so we're gonna say I'm gonna use vim as my text there But as always use whatever editor you prefer And I'll just call it code dot py As always we always start our codes with a shebang line letting our operating system know what Interpreter to use to Run this code. It's not something that's required, but It's a pet peeve of mine just do it It's so people don't have to type Python every time they want to type your code They can just run the code and it knows that it's a Python code So let's create an object here. That's going to grab the information from our file I'll call it f for file and we're going to open and And we're going to open our file which we called file dot txt Okay, so now that we have that open now. We need to go through it line by line We're gonna create two variables in here one that checks the line number and one that is the actual text from the line It's gonna say for and we'll just say x. We're gonna say line x comma line in and we're gonna say Enumerate and then our object that we created which is f And give it a colon so now we're going to be reading through that line that that file line by line our x is going to be the line number where line will be the Text from that line so Now we're gonna say if x Equals equals so if x equals we'll say three Colin what are we going to do? Well in this case? We'll print the line will print line So that'd be the text from the line So you might be thinking we're going to print line three. Let's have a look at that But we want to remember to close our file so f.close And don't forget your parentheses there. We will save this We will change it and make it executable All you have to do that once and then we will run our script dot slash the name of our script And it says line for Now why does it say line for? because you have to remember that The way we're counting Let's open up our text file here. So we'll say file that txt We've got line one line two line three But you got to remember that computers start counting from zero really we all count from zero. We just don't always say it So zero one two three and that's why we got line four there So you can do two things you can either remember that to Take a number off the actual line that you want or put into your code to subtract one from i each time before you Check the if statement But let's go back into our code here and add a little bit to it We're going to say in this case if three which would be the line for we can also say Lf x equals two In this case, we're printing the line You can also do other things when it finds that line But in most cases for what we're looking at you're actually looking to print those particular lines We'll save that we'll now run our code again and this time we got Line three line four and that's how you can go through a file and Indicate what lines you want to print Doesn't really matter what number you put the order to probably be more appropriate to go Numeric order this way to go to then three, but our script is going to check it each time anyway So if it doesn't see three it's going to go to the next thing which would be two So it doesn't really matter the order It's still going to print them in the order that they are in the text file So you can do as many of those as you want Then you can see three four and five So doesn't matter the order of the if then statement or if else statement because It's still reading through the file in the correct order might be a little more Appropriate to put the lines in that in the order in the code just to make it easier to read So very basic very simple little look at this And I hope that you enjoyed the tutorial check out films by Chris comm That's Chris the K those should be a link in the description of this video if you're not already watching it on my site Also visit me on Facebook. I have a Facebook fan page linked to that in the description of this video as well Also, you can check out my Twitter account if you go to filmed by Chris comm There's a little toolbar of that on the side there And if you enjoy my tutorials my videos and you want to see more Think about making a donation to my site. There is a donate button on my site I thank you for watching and I hope that you have a great day