1
00:00:00,550 --> 00:00:01,450
Hey, welcome back.

2
00:00:01,480 --> 00:00:08,680
This video is one of those videos where we don't improve the program output, but we improve the code.

3
00:00:08,950 --> 00:00:10,270
What do we mean by that?

4
00:00:10,300 --> 00:00:14,020
We will make the code more readable, more efficient.

5
00:00:16,370 --> 00:00:18,800
So nothing on the output will change.

6
00:00:19,070 --> 00:00:25,430
What we will do exactly is we're going to use a better way to handle the files.

7
00:00:25,670 --> 00:00:34,010
So, for example, here we are opening a file in read mode than we read the lines of that text file.

8
00:00:34,190 --> 00:00:39,830
We store the lines in the lists and then we close the file.

9
00:00:42,020 --> 00:00:47,870
But this can be done in a better way using the with context manager.

10
00:00:48,770 --> 00:00:52,550
So from this we're going to use with.

11
00:00:53,650 --> 00:00:55,210
And here we place.

12
00:00:56,450 --> 00:00:57,320
This function.

13
00:00:57,320 --> 00:00:58,190
So I copy that.

14
00:00:58,190 --> 00:01:05,780
I place it in here and then this variable will go at the end using the SE.

15
00:01:06,830 --> 00:01:08,660
Key words as file.

16
00:01:09,830 --> 00:01:16,700
And then you use a column which denotes that the next line will be indented.

17
00:01:16,700 --> 00:01:19,060
So this is indented under this width.

18
00:01:19,100 --> 00:01:25,160
BLOCK So this is a block, and everything that goes underneath it is indented.

19
00:01:25,160 --> 00:01:27,200
So now what do we want to do now?

20
00:01:28,920 --> 00:01:31,980
Well, perhaps we want to read lines from that file.

21
00:01:31,980 --> 00:01:36,390
So whatever you want to do with that file, you want to write it here.

22
00:01:36,420 --> 00:01:40,500
In our case, it is to do equal to file that read lines.

23
00:01:40,500 --> 00:01:46,530
So this one, I'll just copy and paste it here and that's it.

24
00:01:47,640 --> 00:01:53,160
You don't need to close the file, and that is the benefit of this.

25
00:01:54,550 --> 00:01:56,770
With context manager.

26
00:01:59,310 --> 00:02:00,990
So I'll delete that line.

27
00:02:02,860 --> 00:02:07,240
And if you run the code now, you're going to get the same exact output.

28
00:02:07,250 --> 00:02:08,740
So nothing will change.

29
00:02:12,820 --> 00:02:19,540
So this method also makes sure to avoid some problems with file handling, for example.

30
00:02:20,350 --> 00:02:22,150
What will happen here is that.

31
00:02:24,300 --> 00:02:26,070
The file is open.

32
00:02:26,070 --> 00:02:32,400
And then even if the the program stops for some problems.

33
00:02:33,870 --> 00:02:36,750
The file will be closed by this method.

34
00:02:36,750 --> 00:02:43,090
But in the other case where we had this file equal to open file write lines.

35
00:02:43,380 --> 00:02:48,510
If the program stops at this point, then the next line will not be executed.

36
00:02:48,600 --> 00:02:55,080
So file that closed will not be executed, and we will end up with an open file which could cause problems.

37
00:02:55,080 --> 00:03:00,810
So this method, this block of code makes sure that that will not happen.

38
00:03:00,810 --> 00:03:04,200
So this is always recommended over this.

39
00:03:05,250 --> 00:03:10,890
Of course, first I wanted to show you this because you understand the logic better, but then we want

40
00:03:10,890 --> 00:03:11,610
to change that.

41
00:03:11,610 --> 00:03:15,990
So let's change this other block of code to a with context manager.

42
00:03:17,360 --> 00:03:18,770
It to be with.

43
00:03:23,400 --> 00:03:24,930
That's as file.

44
00:03:28,480 --> 00:03:30,880
And then that goes in here and that's it.

45
00:03:30,880 --> 00:03:32,470
So delete that.

46
00:03:34,270 --> 00:03:37,870
The code also becomes a bit shorter, as you can see.

47
00:03:39,580 --> 00:03:42,190
Let's do the same in here with.

48
00:03:44,840 --> 00:03:47,510
That as file.

49
00:03:51,760 --> 00:03:54,070
Do that and delete that.

50
00:04:00,870 --> 00:04:02,910
And that's what we have.

51
00:04:04,310 --> 00:04:06,010
So let me test this.

52
00:04:06,020 --> 00:04:11,480
If everything is working fine, so show that we will show what we have so far.

53
00:04:12,140 --> 00:04:16,370
Add a new to do, right, for example.

54
00:04:16,519 --> 00:04:21,070
That's a new to do that I've just added show right.

55
00:04:21,079 --> 00:04:23,270
Is part of the to do list.

56
00:04:23,420 --> 00:04:25,700
So everything is working fine.

57
00:04:25,820 --> 00:04:31,940
So we just modified the show, the ADD case and also the showcase.

58
00:04:31,970 --> 00:04:40,280
Now, we still haven't implemented the changes in the edit and the complete cases, so these are still

59
00:04:41,270 --> 00:04:44,090
not connected to our text file.

60
00:04:44,180 --> 00:04:49,330
They are not connected to our actual data, but we will do this in the next video.

61
00:04:49,340 --> 00:04:50,450
So I'll see you there.

