Hey, welcome back.
This video is one of those videos where we don't improve the program output, but we improve the code.
What do we mean by that?
We will make the code more readable, more efficient.
So nothing on the output will change.
What we will do exactly is we're going to use a better way to handle the files.
So, for example, here we are opening a file in read mode than we read the lines of that text file.
We store the lines in the lists and then we close the file.
But this can be done in a better way using the with context manager.
So from this we're going to use with.
And here we place.
This function.
So I copy that.
I place it in here and then this variable will go at the end using the SE.
Key words as file.
And then you use a column which denotes that the next line will be indented.
So this is indented under this width.
BLOCK So this is a block, and everything that goes underneath it is indented.
So now what do we want to do now?
Well, perhaps we want to read lines from that file.
So whatever you want to do with that file, you want to write it here.
In our case, it is to do equal to file that read lines.
So this one, I'll just copy and paste it here and that's it.
You don't need to close the file, and that is the benefit of this.
With context manager.
So I'll delete that line.
And if you run the code now, you're going to get the same exact output.
So nothing will change.
So this method also makes sure to avoid some problems with file handling, for example.
What will happen here is that.
The file is open.
And then even if the the program stops for some problems.
The file will be closed by this method.
But in the other case where we had this file equal to open file write lines.
If the program stops at this point, then the next line will not be executed.
So file that closed will not be executed, and we will end up with an open file which could cause problems.
So this method, this block of code makes sure that that will not happen.
So this is always recommended over this.
Of course, first I wanted to show you this because you understand the logic better, but then we want
to change that.
So let's change this other block of code to a with context manager.
It to be with.
That's as file.
And then that goes in here and that's it.
So delete that.
The code also becomes a bit shorter, as you can see.
Let's do the same in here with.
That as file.
Do that and delete that.
And that's what we have.
So let me test this.
If everything is working fine, so show that we will show what we have so far.
Add a new to do, right, for example.
That's a new to do that I've just added show right.
Is part of the to do list.
So everything is working fine.
So we just modified the show, the ADD case and also the showcase.
Now, we still haven't implemented the changes in the edit and the complete cases, so these are still
not connected to our text file.
They are not connected to our actual data, but we will do this in the next video.
So I'll see you there.