When it comes to opening files with Python,
remember that we don't always have to use the with statement.
The reason why we use with most of the time is because with, with
statement
automatically the file will be closed once it has been accessed in
this scenario right now,
where we're simply working with the open function to open up the file,
we have to manually close the file by saying file that close,
otherwise the file will remain open and that could lead to potential errors.
So
the question right now is how would we handle for errors in
this particular kind of scenario? Well, we'll have to make use of the finally block.
Now, at the very top,
I'm going to start off with try
as usual. And then remember, of course, everything has to fall on the try.
And now on line five, this is where we're going to add the accept.
And now let's look for the file, the file not found error.
OK? Colon.
So if the file has not been found,
we can simply print file, not found.
However,
what if the file was in fact found
there is no error,
what we're gonna do right now is we're going to say finally,
OK?
And now
if file, hold on. So now we're saying
finally, if there was no error message at all, if the file was in fact found,
what do we now do,
we can simply print out the content.
And now we can also close the file.
And in fact, for good measure, not necessary, but for good measure,
we can just simply print
uh file
closed
just as an example. And of course, if I run the program right now,
you can see it works perfectly fine
and it even says file closed
this program
on the surface right now looks good.
There are no arrows, file was found,
it's printed it out.
And of course, the file has also been closed.
However, we're not done yet
because in fact,
if I was to deliberately change the name of the file
so that we do get the
file, not found error
if I run my program.
Uh oh,
you can see right now
we're still having the usual
uh unpleasant way of displaying or handling our errors.
It's like the try except block of code did not work at all.
What is going on here.
Another thing you need to keep in mind is
that if you're not working with the with statement,
not only would you have to manually close your file,
but you also have to initialize the file at the very beginning
because you see right now as it is,
Python doesn't even really know what file itself is.
Like, what's the default value of file?
A file
isn't equal to uh example that takes like if the file wasn't found,
how would Python know that? OK.
There's actually an error in here like what will be the default value
that the file variable needs to have.
So this is where
we will have to at the very top say file equals none.
Right now, at the very start, we are initializing the file variable,
we're assigning it to the default value of none.
And this is a common practice when dealing with
resources that may or may not be successfully allocated.
So using non here
would allow for a very safe default value that
will prevent any accidental operations on an uninitialized variable.
So this is another thing you have to keep in mind. So right now,
if I run the program one more time, OK, it works well.
If I change the name of the file deliberately to, to produce the error message,
you can see right now it handle the error
gracefully.
So
this is exactly why
when it comes to handling files,
we typically use the with statement because with the with statement,
you don't have to initialize your variables or your files
and you don't have to close them manually. Either
the word statement is so powerful, it will automatically
initialize our files and variables and also close
the file automatically once it has been accessed.
But nevertheless,
you should be aware of this if you find
yourself in a scenario or in a programming code where
the width statement wasn't used. How would you handle errors?
How would you close the file manually?
And of course, how would you also initialize the file manually?
Thank you for watching. I will see you in the next class.