Did you like how we did? Rate your experience!

Rated 4.5 out of 5 stars by our customers 561

Video instructions and help with filling out and completing Where Form 8815 Filers

Instructions and Help about Where Form 8815 Filers

Hey there how's it going everybody in this video we'll be learning how to work with file objects in Python and some of the useful things that we can do with these objects so whether you use Python for desktop or web applications you're likely going to be interacting with files a good bit so it's definitely a good skill to have to know how to properly interact with these file objects okay so let's go ahead and dive in so to get a file object we can use the built-in open command so I have a file here called test.txt in the same directory as my python file now if I open this file we can see that it's just a plain text file with multiple lines so let's see how we can open and read this file from within Python now the way I'm going to open up the file right now isn't the way that it's normally recommended it's usually recommended to use a context manager which I'll show you here in just a second but to show you why a context manager is useful let me first show you this method for opening files first so what we're going to do here is we're going to say F equals open and we're just going to open that test dot txt file now if you're working with files from different directories then you're going to have to pass the path to that file into the open command but since this file is within the same directory as my Python file then I can just pass in the name of the file but if you want to learn more about how paths work then we touch on that a little bit in the tutorial I did on the OS module okay so the open command here allows us to specify whether we want to open this file for reading writing appending or reading and writing now if we don't specify anything then it defaults to opening the file for reading but I usually like to be explicit here so let's go ahead and say that we want to open this file for reading and we can do this by passing in a second argument here and that's just going to be the string of a lowercase R and we'll touch on some of these later but if I wanted to write to a file then it would just be a lowercase W that I pass in appending to a file is a lowercase a and if I wanted to read and write to a file then I could do an R plus but for now we just want to read the contents of the file let's just pass in a lowercase R okay so now the file is actually open and we can print the name of the file if I was to do a print F dot name and also before I run this and print the name of the file out there's one more thing that we have to do here if we open a file like we just did here then we need to explicitly close the file when we're done using it and to do this I'm going to do it by saying F dot close so now we've closed that file let's go ahead and run this and you can see that it printed out the name of the file that we opened and so this has some more information that we can print out also if we wanted to print the mode that the file is currently opened with I can do a F dot mode and if I run that you can see it prints out a lowercase R because we open the file for reading okay so now even though that this works the way that we've just now done this let me show you how to instead open the file using a context manager and why for most use cases you'll want to work with files this way so if we open the file like we did here then we have to remember to explicitly close the file if we don't close the file then you can end up with leaks that cause you to run over the maximum allowed file descriptors on your system and your applications could throw an error so it's always important to make sure that you close the files that you open so in order to use a context manager then it's kind of similar but we can do this using the width keyword so we can say width and then I'm just going to copy all of this so with open test.txt in read mode and then here at the end I'm going to say as F and then I'm going to put in a opening for our block here now this can be a little confusing to people at first because the variable name is actually over here on the right using as F instead of over on the left when we said F equals open but the benefit of these context managers is that they allow us to work with files from within this block and after we exit that block of code it'll automatically close the file for us so we don't have to worry about whether or not we add in these closes here now this will also close the file if there are any exceptions that are thrown or anything like that so that's why using these context managers are considered a best practice and it just automatically takes care of all that cleanup for us so now I'm going to go ahead and delete my outside open and close statements there now one thing that some people don't realize is that you actually have access to this F variable for now I'm just going to say pass within this context manager now we actually have access.

If you believe that this page should be taken down, please follow our DMCA take down process here.