Python Programming Tutorial #13 - How to Read a Text File

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back this is the 13th video in my Python programming series and today we're going to be talking about reading from a file so specifically a text file and we're going to be using something called file dot IO in Python so the first thing we need to do before we can read from a file is we need to create a file so I'm just simply going to go and find the directory where my Python tutorials are so oops not here they are right here in the tutorials folder and now you see I've got all my Python scripts right here and all I'm going to do is I'm going to right click and I'm going to click new and then I'm going to find text document like that now I can name whatever I like I'm just going to name it file for right now file dot txt now you may want to populate the text document so put some words in here so I'm just going to put a bunch of different words lo Tim Python learning easy just some random random words in my text file I'm going to save that and now we can go to reading in the text file so it is important that when you make the text file you do have to save it in the same directory as your Python script so if I were to put the text file here on my desktop and I have my Python script in the tutorials folder here it's not going to be able to find the file properly you would have to do something which is a path joining which I'm not going to talk about in this video but maybe in a later one so make sure that you save your file in the same directory and same folder as where your script is ok so now that we've done that we're going to read from the file so the first thing we need to do is we need to create a variable so we're going to name this file you can name it whatever you'd like and you're going to set it equal to the keyword open and then inside of open all you're going to do is you're going to type the name of your file so file dot txt a comma and then the mode you want to open your filing so in this case we want to read it so we're simply going to put a lowercase R inside of the quotations just like that and now that's pretty simple that's all you have to do in Python to open up the file and to prepare to read it now make sure you do put our in here if you put nothing or you put a W for example which would be right mode it's actually going to clear the whole file which you don't want to happen you want to leave the contents of the file in there and so we need to open it in read mode with this are that's very important now what we're going to do is we're simply going to read all of the things from the file so we've opened the file now we need to read it so in order to read the file we have to type a few things so we can do it in multiple different ways the first thing I'm going to show you is this so I'm going to make another variable I'm going to call it F and this one we're going to make it equal to file dot read lines like this okay and then we can simply print out F just to show you what's going to happen you see we get all of the words that are in the file now there's an interesting thing here you may notice that there's a backslash n attached to all of the words except for the last one that's because I'm going to open up the text file again and show you you notice here I didn't actually type backslash n but every time we click enter like this we actually create something called an escape character which is that backslash N so if we want to read in the file we're going to have to remove that character afterwards right so it's just something to keep in mind that if you have things on different lines each of the end of the lines is going to have a backslash n on that line that you don't actually see here in the text document but Python will read that in okay so we don't need to save that yeah so let's move into the second way to read it properly now so we have F equals file dot read lines now we want to remove all of these back slashes get rid of these ends from our lines so there's a way we have to do that it's by using a for loop so we're going to do a for loop I'm just going to say for line in F because now F has read all of our lines so we're going to do what's called iterating by item if you remember from my other videos so each line so every item in this F list here that just created by file dot read lines we're going to remove that character and we're going to create a new list so let's make a new list we'll a new list just make it a blank list like this new list and then here we're going to say what is it new list dot append and if you remember what append does that simply means add to the end of this new list and then we're going to add the line and we're going to use the slice operator to remove that backslash n so we're going to simply do the colon and then negative one now what this does is will it will have to take all of line except for the last character so negative one I didn't talk about this in the other video but negative one pretty much just goes to the last character but does not include the last character when you do negative one like this so just just follow along for now you don't have to completely understand it but just put that negative one and now if I print out a new list we'll see what we get maybe less f5 and you see we get rid of all of the backslash ends now we have one issue here down here on E's it should say easy but we've actually removed the last backslash n from that string so if we don't want to do that now we need to actually add an if statement into our for loop so inside of the for loop we know type if and then we'll say line and we'll put a negative one again to represent the last character in that line equals equals backslash N and then we'll simply tab this in because remember everything in Python needs to be indented properly for it to be read and now we'll try this and we'll see if this works and there we go so we get hello Sam Python and learning but now we've forgotten one thing we haven't actually included easy so now we need to do an else statement so we have else and then we'll do new list dot append and we'll simply append the line just like that and now we can print it out and we'll see what happens and we get hello Tim Python learning and easy now there is an easier way to do this I just want to show this example because you may want to do something where you're checking through all of the lines and you want to see if the last character or maybe first character is equal to a certain item and depending on what that string is or depending on what it is you may want to add it to a new list you may want to do something to it so I just want to show you a way that we can do that pretty much this is iterating through every line in the file so file here so f creates a list of every line and then we go through every line of it and we see if we say if the end of it has this backslash n then we're going to remove the backslash n otherwise we'll just add that line into our list right okay so now the easier way that we can do is is actually with something that's called the strip string method so in my other video I talked about this strip method that removes the spaces but what we can actually do with this is we can remove what do you call it sorry we can remove the backslash n with the strip method as well so all we have to do here now it's a lot simpler to do a new list dot append and then inside of here we're simply going to type line got stripped and now this will automatically remove those backslash ends and we'll see if this works there we go so we get everything and you can see it's a lot simpler and we don't even have to do the if statement for the easy like that so that pretty much is how you read a file in using Python there are some other ways to do this but this is the most basic and easiest way to understand we also talked about how to remove that it's called an escape character at the end of the string that backslash N and in the next video we're going to talk about writing to a file I hope you learned something if you did please like and subscribe and I will see you again in another video
Info
Channel: Tech With Tim
Views: 51,007
Rating: 4.9788361 out of 5
Keywords: python tutorial, learn python programming, python tutorial for beginners, python tutorial 2018, python programming tutorial, python programming language, software development, programming tutorial, tech with tim, reading a text file
Id: 0EgSo7hsRWM
Channel Id: undefined
Length: 8min 17sec (497 seconds)
Published: Wed Jun 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.