Understanding Python: File Input and Output

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back to understanding python my name is jake and today i'll be teaching you how to read from and write to files file input and output is an essential skill for python programmers having a strong understanding of this subject will allow you to work with persistent data and interact with your file system as we go if you learned something new let me know by liking the video now let's get started working with files in python is pretty easy and the main way that we're going to do that is by using the built-in function open this is going to take a couple of arguments the first thing is going to be the path to the file that we're wanting to open on the right you'll see i have a file in this directory called names.txt i can simply put names.txt as the argument and that's going to be a string you can either pass in an absolute or relative path to where your file is located for example if this file was in a subdirectory called files i'd put files slash names or if it's one up from where i'm currently at i put two dots but since this is in the current directory i can leave it as is and what the open function is going to return is a file handler object we'll store that to a new variable called names file and set that equal to the result of calling open so after this line runs python will open the file and then give us control of that through this names file variable and since this is the manual way of opening a file you'll always want to make sure that you close the file out when you're done with it if you're doing things manually like this get in the habit of closing it out as soon as you write the statement to open it and then you can fill in the code that you want in between later this way you never forget to close it out closing effectively writes changes to the file and python then releases control of the file now there are a number of different things that you can do with this file handler object and a few properties that you can access as well the first i will show is you can access the name of the file itself we obviously know that it's called names.txt because we explicitly pass that in but if you programmatically generating names or iterating through a number of possible names in a list you can use the dot name attribute from named file to get its actual name that way you know what you're working with and we'll print this out so if i run this now after saving and making sure i'm in the right directory we'll run this and we'll see file name is names.txt well that's not very exciting yet as we're more interested in the contents of the file well there are a few main ways to get the contents of a file once you have this file handler object the first is going to be the read method so we'll store the result of the read method in a variable called all names and for that we'll say names file dot read the read method can take a single argument and that is size expects an integer as the size for text or ascii files like this the size will be the number of characters you want it to read if you don't pass in a specific size then it will read everything at once which is why the variable is called all names we'll get more into passing a specific size later on in the video but for now we're going to read everything and after we've read everything we're going to print all names let's give ourselves a little bit more room save it and run it and here again we see file name is names.txt and then we have all the contents of that file exactly what you see on the right now there's another point to be made about reading things in manually like this and the effect of calling read which i'll demonstrate now we're going to print reading more lines after that we're going to print a result calling names file dot read line now the difference between read and read line is that read line will go to the end of a particular line so while you may not see it here in the file there's actually a new line character at the end of each of these lines which lets the file know when it should break the text up python looks for that new line character to determine when a line should end so read line is going to read until it finds the end of the line or that new line character and then stop we'll do this two times we'll save it and run it but what's going on down here we see reading more lines and then we have two blank lines well that's because when you read something in a file python keeps track of your position so when we've read everything with no size specified python read the entire file so at that point it is at the end of the file so when we tried to read two more lines had nowhere else to go and as a result it gave us nothing back to further demonstrate we're going to say print namesfile.readline or end of file and we'll do that for the second one as well and now we see our two lines down here say end of file because nothing was returned i read line more accurately nothing truthy was returned because it returned an empty string now before we dig into that a bit further there's another nifty attribute from files you can check just to make sure you have everything cleaned up and that's a check to see if the file is closed and that simply names file dot closed it's an attribute and not a method call so if we run this at the end we see file closed true okay let's take a closer look in ipython we'll do the same exact thing as we did before names file is equal to open it's going to be names dot text now we have names file a names file is technically a text io wrapper or a file handler object in its attributes at the top we see its name is names.txt the mode that it's in is r more on that later and then we can also see the text encoding as well earlier we stored all the names in the file in a variable called all names and we did that by calling read on names file and now we see exactly what that names file looks like instead of having the line breaks as printed out before remember how i said there's actually a new line character at the end of each of these well we can see that in python jake and then the slash n that's the new line character same thing after amanda kiara all the way into the end when you perform a straight up read like that all the new line characters are preserved in the text that you get back alright getting out of ipython we'll title this section manual open and close now if you don't like having everything jumbled into a single string with the new line characters in place you can use another method of that file handler so do the same setup as we did before names file equal to open names.txt and this time we're still going to use all names but all names will actually be the result of read lines and what read lines is going to do is it's going to give us back a list and where it's going to separate the elements of that list is on those new line characters so the elements of the list i'm expecting out of this call would be jake amanda kiara vanellope etc each an individual element of the list so we can print that out first and let's actually make that a little bit nicer with a nap string and we'll also print out that we're printing out one at a time and just like all normal iterables like lists we can simply do or name in all names print name and then make sure that we close this file and here we should be good to go so we'll save it and run that oh look at this first we see where we started all names and here we see the list jake amanda kiara etc all the way to the very end then we see printing out one at a time jake and an extra new line there same thing all the way down to the end why are we getting that extra new line well two things are going on here one you'll see that we have a new line character at the end of this so while it splits on the new line it does include that new line at the end of each element and another thing is that the print statement by default ends with a new line character luckily this is a behavior we can change so where we have print name here we add a comma we see that we can also put end and here we're going to end it simply with an empty string so we're telling it to instead of put a new line at the end put nothing at the end because we already have a new line so we've saved and ran that and now that looks much better printing one at a time jake all the way through hunter and then horace we'll title this section read all lines into a list alright so you're getting the hang of using open and close on files but you don't really feel like having to open and close that manually every single time this is where some python magic comes into play and that magic is in the form of what's known as a context manager so i'll show you what this looks like and then explain exactly what's going on with open same thing names dot text as names file and then you see we have an indentation here we'll print reading three and then print again names file dot read line we know this is also going to include that extra new line character so again we'll end with an empty string since we want to do this three times we'll do that now all right what's going on here what is this with keyword and as keyword when you see with and as in python that means you're working with what's known as a context manager a context manager handles some kind of process and gives you back an object as a result while you're inside the scope of this contacts manager you can do things with that particular object and then when you exit the scope of that context manager say here the context manager will do some type of step to clean that up for open this top line is going to open the file just as before as we did on line 17 and give us that file handler object stored in a variable called names file we can then use names file within that context manager and instead of having to manually close it out once we're outside the scope of the context manager namely this with block it's going to close it out for you so if you use this context manager version which is the recommended way of dealing with files in python you don't have to worry about closing files out manually this relieves that burden of accidentally leaving a file handler object live let's save that and run it and here we see the result we have our three lines but something kind of nasty here at this top you'll notice that there's no separation between horrors and reading three names and the reason behind that is because in the file there is no new line character after horus the file just ends at the s character if we simply hit enter we'll add that new line character and if we save and run this again we now have that separation there perfect so with our context manager we have reading three names then we have those three names each as a result of calling readline remember that when we read before python remember the position that we are at so after read the first line it knew that it was at the end of the first line so advancing from the end of the first line it has the second line and then the third line this way you're not reading the same thing over and over again we'll call this little section introducing the context manager but it gets even more simple than this we don't have to manually call readline for each line that we want to go through in fact again we use this open context manager opening good old names.txt as names file we'll print that we're going to read all names and then we're going to say for name in names file print name and again we're going to end with an empty string and then exit the context manager this shows you how neat that file handler that was stored in names file is because it'll also let you iterate over that file when we say for name in names file we're effectively saying for line in names file because each time we go through that for loop it's going to read another line just like we did before the difference being that it's going to keep going until it reaches the end of the file and we never had to call readline manually let's run that and here we see reading all names and then jake through horus beautiful if i didn't have this print statement here this would take just three lines of code not only that but this is incredibly memory efficient because it doesn't load the contents of the file all at once like we did with read it's just going line by line or new line character by newline character so you can have a massive file and read through it incredibly efficiently we'll call this section iterate line by line all right aside from just reading from files we have a few other modes available to us we use w for writing a for appending meaning instead of starting at the beginning of the file python will go to the end of the file we can also put a plus after r to both read and write to files and how do we specify that well after we passed in names.txt which is the location of the file we're wanting to read earlier we could have also passed in an r r for read if you're just reading like this you don't have to pass in the r because that's the default value however if you want to write to a file you're going to want to pass in a w so here we'll open up a new file literally called new file.txt we'll go and put a little underscore as well and after new file.text the mode that we're going to pass in is w write and we're going to store that in a new variable called nf so that nf will become our file handler object now one thing to be careful of if you're using the w or right is that if that file exists it's going to erase the contents of that file so for example let's go ahead and put some stuff in here now mi still here and the file we're going to save this to is new file.text matching the one we're wanting to write to in just a few minutes and we'll save it now back in our python code i'll show you how to write to a file manually and for that we're going to use the write function nf dot write and in here we're going to pass in a string and we'll do this a couple of times these lines nf.right are not separated in f dot right and we're going to put a new line character here use new lines to separate and let's save this now as i run this pay careful attention to what's going on in the right here and since we are just straight up writing to the file we're going to get our answer here we go and there it is our question of am i still here has just been answered and that answer is no we open the file in write mode which means that everything that was in it is now gone luckily for us that's nothing we actually cared about and now also look at the behavior on the right we wrote to the file three different times a lot of people when they're starting out expect these to be written to different lines but that isn't the case as we're not actually writing new line characters to those lines it's not until we manually put those new line characters in our strings that they show up in the file you can see that new line character implicitly here as well as at the end which is why we have this extra line here now not only can we write just from a single string you can also use the right lines method and write lines we'll write more than one string and what i mean by that well rightlines expects an iterable like a list and the contents of the iterable should be strings and what we'll do is we'll go through the iterable and write each of the values to the file so let's demonstrate this nf dot write lines and in here i just want to have the numbers zero through five but remember that i said it still expects it to be a string well we can easily do that using the map function we're going to convert each of those values into a string if you're confused about how this works there should be a link in the top right now pointing you to my video on map and filter give that a watch as these functions are also very valuable we'll see what this looks like and that's correct i did say i wanted to go zero through five so we'll bump range up to six as if you remember range's endpoint is not inclusive we'll try that again and here we go zero through five that's all well and good however you still need the new line characters for that we can just modify that map that we did above so instead of straight up mapping a string we'll throw in a laminate function that's going to take an s and return an f string throwing in the value of s followed by newline character we'll save and run that and here you see you still need new line characters and then each of those is also separated by a new line character we'll title this section writing to files notice that each time that we did that it overwrote the contents of this file if you don't want that behavior if you wanted to start at the very end of the file you would switch this mode up to appending with an a here if we run that now you see that it added that to the end of this and it'll keep doing that each time you run this adding it to the very end of the file not overwriting the data you did before this is something that you'll commonly see in loggers always writing to the end of the file if we switch that back to our w then it's going to overwrite all those contents again and we're back at where we were okay up until now all files have been opened in what's called text mode that means we read and write strings from and to the files but there's another mode we can use binary mode to do that we add b to the mode now when we do this data will be read and written in the form of bytes object this is the mode you should use for all files that don't contain text or shouldn't be interpreted as text and that looks like this again we're going to use the context manager we'll call this file binary file we're not going to put an extension and the mode we're going to use is wb is wb because we still want to be in writing mode but we also want to be in binary mode notice how these are additive you could do the same thing if you wanted to read in binary mode and append in binary mode we'll stick with w for now and we're going to have this be b file or binary file and similar to what we did before we'll also use the right method this time instead of a normal string being passed in we're going to put a b in front of it that tells python that this should be a bytes type string and before i make a silly mistake let's make sure we separate those arguments with a comma so it doesn't combine them together now if we look in our directory we should have a new file called binary file let's move that over to our right and here we see 0 through 9. i'm not going to use anything different than normal type characters because that's a little bit harder to follow so far straightforward now let's write a little bit more to the file efile dot right again this is going to be a byte string we're just going to do query u up but this time we're interested in how many bytes were written so we can store that in a new variable called bytes written which is what right gives back so while it does carry out the right here also tell us how many bytes it wrote in the process and we can print that out we wrote bytes written bytes to b file dot name and we'll toss the cherry exclamation point at the end let's run this and here we see add query yui up to the end and we see it says we wrote 10 bytes to binary file 10 is exactly what we're expecting as that is the length of query yuiob all right that wasn't so hard so we'll call this section working with binary files and now that we have this binary file let's play around with it a bit more in ipython give ourselves a bit of room and we're going to go ahead and open that up since we're just going to be exploring with an ipython we can go ahead and use bad variable practices and just call this f f for file we're going to be opening up binary file and this time we're going to be reading in bytes mode but we also want to be able to write to it so we're going to put a plus there this means that not only can we read from the file we can also write to it and this has the very nice side effect of not instantly overwriting the contents of our file now remember before when i said that read has a size that you can pass in or how much you want to read at that particular moment we'll try that out now we're gonna read five we get a byte style string with five characters in there and if you also remember our file handler object keeps track of our position after we do reads like this so we don't have to start from scratch each time well one of the helpful methods of that file handler object is telling us where we're at and does that with the tell method you see that we read five bytes so now we're five bytes in if we read another five bytes we see that work now 10 bytes in but not only can we read in this way we can also jump ahead for that we're going to use the seek method and here we're going to say seek 15 and it tells us our position is 15. now we're just going to read a single character to let us know where we're at when we look at the file and here we can see 15n is y as you might have expect that also advanced us to the 16th position now not only can we seek forward but we can also seek backwards and we'll do that by passing in negative 5 but we want to make sure that the second argument that we pass in is a 1 because 1 stands for our current position there's three possible values you can give for the second argument zero which is the default references the beginning of the file one which is what we're going to use here is your current position within the file and you can also pass in a 2 which represents the end of the file so with the seek we're saying move back 5 bytes from our current position and you see it tells us that we're currently at position 11 and if we read a single byte from that we see that we're now at w and of course because we read that we also move forward that one spot now instead of going from our current position let's try from the end of the file and see where we end up okay this makes sense since our file is 20 bytes long if we move back 5 from the end we're currently at position 15. if you might remember from before position 15 is y now at this point you now know how to navigate manually around the file using seek and read and tell to figure out where you're at and move to particular locations you can do this programmatically reading certain contents at a time and deciding where you want to go from there additionally we can also write to that file here i'm going to write a byte string it just says my name and it tells us that it wrote four bytes but notice that nothing changed in the file that's because that doesn't change until that file is closed or flushed let's demonstrate on the close close the file and now we see jake at the end there we manually overwrite this finish off the query ue up open the file back up we can do our seek again f.seek this time we'll seek negative 3 from the end of the file we're currently at position 17 and then we're going to write jake again and instead of having to close the file to see the changes we're going to call flush which basically says all the changes i've made to the filehandler object go ahead and make to the file and here we still have the file opened you see that app.closed is false but the changes are now reflected in the file itself so let's close that now now we've closed out the file we can verify it's closed by calling closed again and we see that we in fact have closed out the file speaking of closing out the file that closes out this video now that you understand file input and output you can apply this in countless ways what is your favorite trick when working with files or was it something i showed today leave a comment down below to let me know as always today's code will be added to the understanding github repo check the description for a link and of course if you have any questions or suggestions for topics you'd like me to cover leave a comment for me to keep up with this series please consider subscribing thanks for watching [Music] you
Info
Channel: Jake Callahan
Views: 92
Rating: undefined out of 5
Keywords: understanding python, python file input and output, python, python file io, file input output python, python files, python file i/o, python file input, python input output, input and output, python open file, file input, file output, python read file, python write file, learn python, codecademy python file input/output, python 3, python for beginners, file handling in python, python 3 standard input, file handling in python 3, python file handling
Id: gAC7tE7ZTRQ
Channel Id: undefined
Length: 30min 51sec (1851 seconds)
Published: Mon Aug 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.