Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Demo: Safer file handling

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Loadings the rolls worked great but there's a few things that we need to work on. First of all, notice this.
0:07 Now this is a topic we've not covered at all we've experienced it, but we haven't covered it. If there's a problem with some code in Python
0:15 what happens is Python throws what's called an Exception and that means if you're coming down here, and here
0:21 and like right on this line there's an Exception it's not like this comes back as nothing. It's going to stop what it's doing
0:26 figure out if there's some kind of error handling block it's not here, so it's going to go see who called this and it's just going to leave
0:33 it's just going to stop on line 95 and carry on. You know, maybe worse if so the error happened on 36, or 96
0:40 the file's going to be open, and we're going to leave and that could be a big problem. So we want to be safer about that.
0:45 We want to make sure that no matter what even if there's an error we call this close function. So we can, there's a different way to write this
0:53 these three lines of code here. Check this out. This is a new construct, it's called a Context Manager. It uses the 'with' keyword
1:00 so you say 'with' whatever we had here as whatever the variable name is, so fin all right take that, put it there
1:08 and then we indent into this block here. Whatever happens in this block is going to be when the file is open and then when we leave
1:18 you can't really see it here, let me put some more you look really carefully see there's a vertical line right there? This is PyCharm hinting
1:27 oh all this stuff is inside the block. Whenever in the with block whenever we leave this and we go back to, like the next thing is print
1:34 it's going to automatically call close. And importantly, it's going to that even if there is an error. So that, that bit right there
1:42 is a safer version of this cause this doesn't check if there is an error and also call close. That would take like three or four more lines of code.
1:50 So we're not going to do that. We're just going to do this. Whenever your working on files always use a with block there.
1:55 I'll leave this in as a comment for you but we're not going to run with that. Just one more time, make sure it works. Yes, it loaded it up all fine.
2:04 Perfect! So, everything is looking great this with block, this context manager is recommended any time you have something
2:12 that you must must must close, clean up blush, shut down whatever as part of you've opened it do some stuff, and now close it
2:19 even if there is an error. This is common in databases if you're writing to a database you need to have a transaction
2:25 you want to either commit or roll it back if there's an error. So it appears in many many places also networks and socket connections
2:31 but files is one of the places you'll definitely run into these context managers so don't forgot to use them.

Talk Python's Mastodon Michael Kennedy's Mastodon