WEBVTT 1 00:00:00.300 --> 00:00:01.460 Hi, and welcome back. 2 00:00:01.460 --> 00:00:03.120 In this video we're talking about 3 00:00:03.120 --> 00:00:05.150 opening and closing files automatically 4 00:00:05.150 --> 00:00:06.440 so we don't have to remember 5 00:00:06.440 --> 00:00:08.880 to call file.close() at the end 6 00:00:08.880 --> 00:00:10.913 of every time we wanna use a file. 7 00:00:11.830 --> 00:00:13.200 So far we've been opening and closing 8 00:00:13.200 --> 00:00:14.890 files explicitly, like this, 9 00:00:14.890 --> 00:00:17.840 saying, file equals open() of something 10 00:00:17.840 --> 00:00:20.720 and then at the end file.close(). 11 00:00:20.720 --> 00:00:22.720 By the way, I'll just say I've copied the code 12 00:00:22.720 --> 00:00:24.130 we wrote in the last section, 13 00:00:24.130 --> 00:00:26.130 and I've created another file for it, 14 00:00:26.130 --> 00:00:27.650 just because I'm going to change it slightly 15 00:00:27.650 --> 00:00:30.540 and I don't want us to lose any of the files here. 16 00:00:30.540 --> 00:00:32.320 These files, again, are all on the GitHub 17 00:00:32.320 --> 00:00:34.050 so you can access them if you want, 18 00:00:34.050 --> 00:00:37.360 and the link to do that is at the start of this section, 19 00:00:37.360 --> 00:00:38.260 the first lecture. 20 00:00:39.420 --> 00:00:40.910 So we've been opening and closing files 21 00:00:40.910 --> 00:00:42.650 automatically like so, 22 00:00:42.650 --> 00:00:46.520 but Python actually has a piece of syntax 23 00:00:46.520 --> 00:00:49.540 which is extremely handy for these scenarios. 24 00:00:49.540 --> 00:00:52.000 And the scenario that this represents 25 00:00:52.000 --> 00:00:54.080 is a pretty general scenario in which 26 00:00:54.080 --> 00:00:57.610 we have to do some setup and some tear down. 27 00:00:57.610 --> 00:00:59.393 The setup is we have to open the file, 28 00:00:59.393 --> 00:01:02.890 and the tear down is that we have to close the file. 29 00:01:02.890 --> 00:01:04.570 A setup is just a key word for something 30 00:01:04.570 --> 00:01:05.840 that happens at the start, 31 00:01:05.840 --> 00:01:08.690 and a tear down is a key word or 32 00:01:08.690 --> 00:01:11.163 a concept for something that happens at the end. 33 00:01:13.210 --> 00:01:15.620 Another example would be opening a database connection 34 00:01:15.620 --> 00:01:17.090 at the start, and at the end 35 00:01:17.090 --> 00:01:18.910 closing the database connection. 36 00:01:18.910 --> 00:01:20.680 So as you can see, opening and closing things 37 00:01:20.680 --> 00:01:22.070 pretty popular thing, 38 00:01:22.070 --> 00:01:25.113 and this is a setup and tear down approach. 39 00:01:26.240 --> 00:01:27.910 The construct that we're going to learn about, 40 00:01:27.910 --> 00:01:29.810 this piece of syntax that Python offers, 41 00:01:29.810 --> 00:01:31.990 is called a 'with' statement. 42 00:01:31.990 --> 00:01:32.940 You may have seen this around 43 00:01:32.940 --> 00:01:35.000 if you've looked at some Python code before, 44 00:01:35.000 --> 00:01:38.130 and they use the with keyword, 45 00:01:38.130 --> 00:01:41.070 and they're also known as context managers. 46 00:01:41.070 --> 00:01:44.230 Context managers are reasonably advanced concepts, 47 00:01:44.230 --> 00:01:45.860 and we're going to look at them more in depth 48 00:01:45.860 --> 00:01:46.693 in the next section, 49 00:01:46.693 --> 00:01:49.080 we're going to be programming some context managers. 50 00:01:49.080 --> 00:01:50.920 But I just wanted to teach you how to use them, 51 00:01:50.920 --> 00:01:52.500 so you start getting familiar with them, 52 00:01:52.500 --> 00:01:55.930 and in the next section everything's gonna be much simpler. 53 00:01:55.930 --> 00:01:58.350 In addition, they simplify our code greatly 54 00:01:58.350 --> 00:02:00.540 and reduce the errors that we can cause 55 00:02:00.540 --> 00:02:02.023 by forgetting to close files. 56 00:02:03.610 --> 00:02:05.573 So instead of doing this here, 57 00:02:07.130 --> 00:02:07.963 we can say 58 00:02:10.700 --> 00:02:14.303 with open() as file: 59 00:02:16.270 --> 00:02:17.353 then we can do this, 60 00:02:18.390 --> 00:02:21.753 and we can do that. 61 00:02:21.753 --> 00:02:24.900 That replaces our previous code, 62 00:02:24.900 --> 00:02:26.140 now we do with, 63 00:02:27.240 --> 00:02:28.560 then we open, 64 00:02:28.560 --> 00:02:30.460 we call the open function, 65 00:02:30.460 --> 00:02:32.680 and then we do as file so that 66 00:02:32.680 --> 00:02:35.800 whatever the open() function returns 67 00:02:35.800 --> 00:02:39.460 goes into this variable, then we can use that variable 68 00:02:39.460 --> 00:02:42.290 when we reach the end of this indented block, 69 00:02:42.290 --> 00:02:44.570 notice the colon at the end there, 70 00:02:44.570 --> 00:02:46.300 when we reach the end of the indented block, 71 00:02:46.300 --> 00:02:49.063 it automatically closes the file for us. 72 00:02:50.380 --> 00:02:53.590 This here is called a context manager, 73 00:02:53.590 --> 00:02:56.360 and it doesn't happen magically, 74 00:02:56.360 --> 00:02:59.700 the open() function has defined that it can be used 75 00:02:59.700 --> 00:03:00.790 with a context manager. 76 00:03:00.790 --> 00:03:03.760 So you cannot just do this with anything you want, 77 00:03:03.760 --> 00:03:06.040 but you have to define that you want to be able to 78 00:03:06.040 --> 00:03:08.900 use a context manager with your stuff 79 00:03:08.900 --> 00:03:10.470 and if you do that, you can start 80 00:03:10.470 --> 00:03:12.053 using context managers like so. 81 00:03:12.896 --> 00:03:14.270 Okay and in the next section we're going to look at 82 00:03:14.270 --> 00:03:16.930 developing and programming our own context managers 83 00:03:16.930 --> 00:03:19.350 but I wanted to show this scenario in which 84 00:03:19.350 --> 00:03:21.700 you can use a context manager without even knowing 85 00:03:21.700 --> 00:03:24.270 how it's programmed to simplify your code 86 00:03:24.270 --> 00:03:25.263 and make it better. 87 00:03:26.290 --> 00:03:29.930 So again, this allows you to open the file at the start, 88 00:03:29.930 --> 00:03:32.080 and automatically close it at the end. 89 00:03:32.080 --> 00:03:33.660 And the closing at the end happens 90 00:03:33.660 --> 00:03:35.820 because the open() has said that 91 00:03:35.820 --> 00:03:37.670 when you reach the end of the block, 92 00:03:37.670 --> 00:03:39.420 the file shall close automatically. 93 00:03:40.600 --> 00:03:42.840 Okay, there's no magic going on here, 94 00:03:42.840 --> 00:03:45.740 the open function is defined to close the file at the end. 95 00:03:46.640 --> 00:03:51.290 Now, I'd encourage you to do the same for this file here. 96 00:03:51.290 --> 00:03:55.020 Replace it by a context manager as that's gonna give you 97 00:03:55.020 --> 00:03:58.203 a little bit more practise implementing this stuff. 98 00:03:59.510 --> 00:04:02.160 I hope you managed, again, all you have to do 99 00:04:02.160 --> 00:04:05.693 is put the with at the start, and then as the variable. 100 00:04:06.610 --> 00:04:09.210 The variable here, file, now gets created 101 00:04:09.210 --> 00:04:11.430 as the result of this open(). 102 00:04:11.430 --> 00:04:13.890 Whatever's indented is going to be in the with, 103 00:04:13.890 --> 00:04:15.290 and at the end of the with, 104 00:04:15.290 --> 00:04:16.870 the file's gonna close automatically 105 00:04:16.870 --> 00:04:19.223 so we don't need file.close(), either. 106 00:04:20.980 --> 00:04:25.010 Okay, notice how for json.loads() or dumps(), 107 00:04:25.010 --> 00:04:26.900 you don't need to do any withs 108 00:04:26.900 --> 00:04:28.610 because there's no file to open or close. 109 00:04:28.610 --> 00:04:30.500 These things are just going straight into 110 00:04:30.500 --> 00:04:32.530 or straight out of strings. 111 00:04:32.530 --> 00:04:34.673 So there's nothing to open or close. 112 00:04:36.430 --> 00:04:37.263 So that's it. 113 00:04:37.263 --> 00:04:40.430 A quick introduction to context managers, 114 00:04:40.430 --> 00:04:41.990 and they're called context manager because 115 00:04:41.990 --> 00:04:44.960 they help you manage the context of your application 116 00:04:44.960 --> 00:04:47.660 in which you now have this file open. 117 00:04:47.660 --> 00:04:49.820 And they manage the context by keeping the file open 118 00:04:49.820 --> 00:04:53.640 during the context manager and not before it or after it. 119 00:04:53.640 --> 00:04:55.260 So that's why they're called context managers. 120 00:04:55.260 --> 00:04:58.270 We're going to learn more about them in the next section. 121 00:04:58.270 --> 00:04:59.340 That's it for this video. 122 00:04:59.340 --> 00:05:00.790 I'll see you in the next one.