Reading Files | Python | Tutorial 28

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about reading from external files in Python now a lot of times in Python you're gonna want to read from files that are outside of your Python file so you might want to read information from like a text file or a CSV file or like an HTML file and you can actually use something called the Python read command and it will allow you to read a file that is stored outside of your Python file so you can use these files to get information or you can parse through different files and do different things so I'm gonna show you guys the basics of reading files opening files closing files doing all that stuff over here I have this file called employees dot txt and it basically just lists out a bunch of different employees like these could be employees in an office or whatever so it's just listing out all of this information so let's say that inside of my app dot python file i wanted to read the employees inside of that file the first thing i have to do is actually open that file from inside of python so i can use a special command called open so i can say open and then in here i can type in the name of the file that i want to open so this is either gonna be a relative path to the file an absolute path to the file or just the files name if both files are in the same directory so in my case app dot python and employees dot txt are in the same folder like they're in the same directory so i can just type out the name of the file I can just say employees dot txt and now I want to put one more thing inside of this open function and it's gonna be the mode that I want to open the file in so you can actually open files in a couple different modes and the first mode is called read so I can just put an R here and that's gonna stand for a read and this basically means that I only want to read the information inside the file I don't want to modify it I don't want to change it I just want to read it I just want to see what's in the file and do some stuff with that information another mode it's called write so I can type in this and writing basically means that you can change the file write you can write new information you can change existing information there's another one called a and a stands for append and this basically means that you can append information on to the end of the file so you can't modify any of the information in the file you can't change any of the information but you can add new information and there's one more which is our plus and this basically means read and write so this will give you all the power of reading and writing so in our case we're just going to be working with regular AAR's so we're gonna be reading from the file now this open function will essentially just open the file so it's gonna like go over to that file inside of our file system open it up and it'll allow us to read through it but generally we're gonna want to store this opened file inside of a variable so I can create a variable and we can just call it employee file and I'm just gonna set it equal to this open function so now the open employees txt file and all the content inside of it is stored inside of this employee file variable now whenever you open a file you always want to make sure that you close the file as well so just like we have this open command we also have an close function so I can come down here and say employee file dot close and this is essentially just going to close the file so we're no longer gonna be able to access it and generally it's a good idea whenever you're opening up a file you want to also make sure that you're closing the file at some point so generally once you're done reading it you can just close it so that's kind of like how we can open and close a file now let's talk about how we can get information from the file right there's no point in having the file that if we can't figure out what's in it so there's actually a few different functions that we can use on this employee file to figure out what's inside of it and I'm gonna show you guys some of those so I'm just gonna make a print statement and inside this print statement will basically just print out some information about the employee file so the most basic thing we can print out is just the entire contents of the file but before I do that I want to show you guys how you can check to make sure that a file is readable so before we do anything else generally it's a good idea to make sure that it's possible to read this file and there's a function inside of Python we can use called readable so I'm just going to type out employee file dot readable and this is going to return a boolean value and it's gonna tell us whether or not we can read from this file so I'm gonna run my program and you'll see down here we get a value of true and that's because we set the file with a read mode so it's in read mode we can read from it if I was to put a W here so if I put like right now readable is gonna be false because we can no longer read the file we can only write to the file so I'm going to change this back to R so we can just read it so once you figure out whether or not the file can be read from let's actually read it so there's another function called employee file read and this is basically just gonna spit out all the information in the files so when I run the program it's just gonna spit out all of this information write all the information that was in that file I can also come down here and we can read an individual line inside this file so I could say employee file dot read line and what this is gonna do is it's gonna read an individual line inside of this file so now when I run this program you'll see it's just reading that first line in the file and this red line function is actually just reading the first line and then it's basically like moving a little cursor onto the next line so if I was to copy this line of code and then print it again down here I'm saying employee file read line so it's gonna read the first line and then when I say it again it's gonna read the line after that so this is actually gonna end up printing out the first two lines in the file when I run this program you'll see we print out Jim salesman and Dwight salesman so if I were to do this multiple times like I could technically print out every line inside of this file and you can see we can do that and so that can be pretty useful for reading multiple lines in a file but there's actually another function that is better at doing that we can say instead of employee file read line we can say dot read lines and what this is gonna do is it's gonna take all of the lines inside of our file and put them inside of an array and so now when I print this out you'll see we have this array down here that says Jim salesman that's the first item in the array Dwight salesman's the second item in the array it's basically taking each line and putting it inside of an array so if I wanted to access a specific line I can just refer to it by its index in the array so if I said one now this is going to give us that Dwight salesman line because that is at index position one inside of the array you can also use this read lines function with a for loop so I can come up here and create a for loop I'm just gonna say for and we'll say employee in employee file and then for each employee we just want to print them out so and actually sorry we have to put employee file dot read lines up here and so now this will loop through all of the employees in this employee file dot read lines array so we can actually just print out the individual employee and now this will print out all the employees inside of that file so it's basically printing out each line in the file and that can be pretty useful so you can use all of these different functions like read read line read lines readable you know there's a bunch of these different things that we can do to get information from a file and so there's a lot of cases where you're gonna want to be able to parse through information in a file and this is a awesome way to do it so just to recap whenever you want to open a file and read from it you can just use this open function type in the name of the file and then the mode which in our case is gonna be R then you can do all sorts of stuff with it and you always want to make sure that you close it when you're done that's just good practice so that's the basics of reading from files and hopefully you guys can use this in some way shape or form in the future hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying tropic Academy and you want to help us grow head over to draft Khadem e-comm forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 27,767
Rating: 4.9653463 out of 5
Keywords: Programming
Id: jC7CPoSDNzE
Channel Id: undefined
Length: 9min 4sec (544 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.