Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Context Manager Example (Opening Files)

Give Feedback

By looking at an opening files example, this lesson shows you the syntax of the with-statement and what you need to write instead if you don’t want to use it. Additionally, the mechanisms working behind the with-statement are revealed.

00:00 A good example here would be dealing with files. An open file is a system resource and you need to acquire it in some way, and then also release it to give that resource back to the system so it doesn’t have to keep track of the file you’re working with.

00:16 I want to take a look at this simple example here, where we’re using the with statement to open a file and then write to it, and to have the file automatically closed. So, the big advantage here is what I just said, where if we use the with statement to open the file, Python is going to make sure that this file is going to be closed once we’re done with it. So basically, when you do this, you create this context where we’re working with the file object, right? We’re giving it the name f, which is maybe not an ideal name, right?

00:50 Maybe you should call this outfile or something like that. Then, in the block that follows—in this indented block here—we can work with f, and as soon as the execution of your program leaves the context, Python will automatically close the file.

01:06 So it’s going to call f.close() and that will return the resource, the file descriptor, back to the operating system and it doesn’t have to keep track of that resource anymore.

01:17 So, to demystify how the with statement works, let’s take a look at what actually happens behind the scenes. So, if you were to write this manually, you would probably do something like that, right? So, you would open the file, just assign it to f here on this line, and then I’m using a try / finally statement here to actually write to the file. And then, to make sure I actually close the file, so even if something goes wrong here—you know, we have some kind of exception, some error where we can’t write to the file—this would make sure that we’re actually calling .close() on the file, which in a longer-running program—that’s super important, because you always want to give up these resources.

01:53 So the try / finally here is really significant, right? It wouldn’t be enough to just say, “Okay, I’m going to do an open() and then a .write() and then a .close(),” because you could potentially leak that resource by not releasing it again if some kind of exception would happen. Using the with statement really simplifies the code we have to write here. Because what you see in the second code example—that is super common. You’re going to use a pattern like this very, very frequently if you’re dealing with any kind of resource like open files, open network connections—stuff like that.

02:23 You always want to make sure you’re using this try / finally pattern. So, the with statement is a way to abstract that away, to factor out that functionality so that you don’t have to write that every single time you open a file or you create a file. Now, how does this actually work behind the scenes, right?

02:42 Because this seems like a really nice feature, and in my opinion, it’s a highly useful feature in Python. But how do we go from this to that, behind the scenes? This is what I’m going to explain to you now.

Become a Member to join the conversation.