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