So in the last lesson,
we looked at how we can use this JSON format to save our data and to load
our data into our program.
But one of the problems that we have is if we run our program from scratch
where we don't have that data.json file created,
then the first thing that it's going to encounter once it lands inside here is
it's going to try and open a file that doesn't exist.
And what happens then? We get a file
not found error because there is no such file data.json.
So what can we do? Well,
we already learned about dealing with exceptions and handling them so that we
don't end up in these situations.
So now it's time to put your knowledge into practice inside a real project.
And I want you to think about how you can catch that particular exception,
how you can get it to work so that if this fails,
then we simply create a brand new data file.
But if it does work, then we can simply load up the data,
update the data and write the data.
So the actual implementation I'll leave up to you.
But what you want to be able to do is to not have a data.json file and run
your code, and it would not crash
and instead it would create a brand new data.json file with the new data.
But if you had already got some old data, then it should simply just update and
write it to file. Pause the video and see if you can complete this
as a challenge.
This always starts with analyzing which part of our code is the part that's
going to fail or likely to fail at some point. And in our case,
it's this line right here,
opening this line in read mode is dangerous because if this doesn't exist,
then we get that file not found error.
So what we can do is we can put this inside a try statement.
Try opening this file and try reading the data that's inside.
Now, if this fails, however, then we can catch it
and we're going to catch a very specific type of exception,
which is the file not found error exception. Now,
when this exception happens,
what we want to do is we want to create a new file
which is going to be our data.json. So at this point,
remember it doesn't yet exist. And we're going to open it in write mode.
And this data file is going to be used so that we can write to it
and we can dump our newly created data up here.
Remember, when the user hits the add button,
then everything they've typed in these entries gets aggregated into this
dictionary
and that dictionary is what we want to dump into this new data file.
So we'll get hold of that new data and we'll dump it into the data file.
And again,
we'll keep the indent to four spaces just so that we can read it more easily.
Now this is what should happen if the file's not found.
But if the file was found and it already exists,
then we're going to tap into the else statement. And in the else statement
what we're going to do is we're going to get hold of the data which we managed
to get because this block of code succeeded, and in the else block
what we're going to do is we're going to update the data with the new data. Now,
this else block is only going to be triggered
if everything inside the try block was successful.
So that means by the end of this block,
we have this data which holds onto all of the existing data from that data file
and we update that data with the new data which is this dictionary here.
Now, after we've updated that data,
we want to be able to open up data.jason in the write mode
and then dump this updated data from here into that data file.
And then once all of this is done,
no matter if there was an exception or if there was no issues,
we're going to still need to delete everything that's inside the website and
password entry.
So we can even add a finally keyword right here.
So this is the final code that deals with our exception.
We have our try block which can fail, we have our except block
which deals with any failures that occur, we have our else block
which has the code that needs to be run if there were no issues,
and finally, at the very end no matter if there was an issue or no issue,
we have a block of code which clears our website and password entries.
Now you might've noticed there's a little bit of repetition here with the
writing to file. So if you want to,
you could in fact create a separate method somewhere up here.
But I'm actually going to keep this as it is just because I think when you look
at this code, if you had any issues and you wanted to review it,
this is much easier to understand and reason about compared to a separate
function.
Feel free to refactor this code and make it as short as you want.
But this is the version I'm going to keep
just so that if you had any issues and you want to take a look at it,
it's there for you
and it's easy to see the try, except, else, and finally blocks with all of the
code together in one place.
Now in the next lesson,
we're going to be completing the functionality of our password manager.
We're gonna add that all important search functionality
which is going to look through our JSON, fetch the results for a particular
website, and then show it to us in a popup.
So for all of that and more, I'll see you on the next lesson.