1 00:00:07,250 --> 00:00:10,860 Welcome back. 2 00:00:10,860 --> 00:00:13,560 We're going to see a little shorthand that makes it even easier to work 3 00:00:13,560 --> 00:00:16,965 with files and avoids the need to remember to close them. 4 00:00:16,965 --> 00:00:20,760 Python has an advanced feature called context managers. 5 00:00:20,760 --> 00:00:23,820 We don't need to worry about all of its generality but 6 00:00:23,820 --> 00:00:27,015 it makes possible this nice recipe for working with files. 7 00:00:27,015 --> 00:00:29,655 It's easiest to understand it by example. 8 00:00:29,655 --> 00:00:36,060 You start with a special word "with" and then there's another special word "as". 9 00:00:36,060 --> 00:00:39,540 In between, you have the open statement. 10 00:00:39,540 --> 00:00:44,725 Then we say, open in the file name and whether you want to read or write it. 11 00:00:44,725 --> 00:00:48,915 After the word "as" you have a variable name. 12 00:00:48,915 --> 00:00:53,520 So, this is actually equivalent to saying, 13 00:00:53,520 --> 00:01:01,170 md equals open of 14 00:01:01,170 --> 00:01:10,800 my data dot txt for reading. 15 00:01:10,800 --> 00:01:17,125 It's equivalent to that in that in the code block that's indented under the word "with", 16 00:01:17,125 --> 00:01:25,605 we can refer to md and md will be bound to our file object. 17 00:01:25,605 --> 00:01:32,025 But we get a behind the scenes action that happens at the end of the code block. 18 00:01:32,025 --> 00:01:35,830 It's as if we have an md dot close. 19 00:01:38,750 --> 00:01:42,240 That gets executed behind the scenes. 20 00:01:42,240 --> 00:01:43,930 We don't actually have to specify it, 21 00:01:43,930 --> 00:01:49,385 it just gets done for us after everything else gets executed in that code block. 22 00:01:49,385 --> 00:01:58,350 If I run this, 23 00:01:58,350 --> 00:02:11,050 I just get a print 24 00:02:11,050 --> 00:02:13,390 out of the first line and the second line. 25 00:02:13,390 --> 00:02:15,660 As you might expect, 26 00:02:15,660 --> 00:02:20,435 since we've iterated through the lines in the file object, 27 00:02:20,435 --> 00:02:25,510 printing each one, we get the printout of those two lines that were in the file. 28 00:02:25,510 --> 00:02:28,610 They're showing up in the output window. 29 00:02:28,610 --> 00:02:34,240 Let's get rid of our markings here and just generalize this a little bit. 30 00:02:34,240 --> 00:02:39,630 Suppose we didn't want to just open my data dot text but we 31 00:02:39,630 --> 00:02:44,350 wanted this to work with any filename. 32 00:02:44,350 --> 00:02:48,680 So let's say fname equals my data dot text. 33 00:02:51,120 --> 00:03:01,605 We will open the file name here and then we would do stuff with the file object. 34 00:03:01,605 --> 00:03:06,060 So, we would have md dot read or md 35 00:03:06,060 --> 00:03:13,745 dot read lines or we might have for line in md. 36 00:03:13,745 --> 00:03:20,530 Do something with each line. 37 00:03:20,530 --> 00:03:25,955 So now we have sort of a general recipe for reading data from a file. 38 00:03:25,955 --> 00:03:31,900 We start with this "with" statement where we open a file object and we assign it to 39 00:03:31,900 --> 00:03:38,855 the variable name md and then we use one of our three methods of working with the file. 40 00:03:38,855 --> 00:03:42,950 We either read all of its data in as a single character string with 41 00:03:42,950 --> 00:03:47,660 dot read or we read all of the lines as a list of character strings 42 00:03:47,660 --> 00:03:51,980 with dot read lines or we just iterate through those character strings 43 00:03:51,980 --> 00:03:57,290 directly using iteration over the file object on line five. 44 00:03:57,290 --> 00:04:02,840 The same recipe will work just as well for writing files. 45 00:04:02,840 --> 00:04:05,500 So, if I change that r to a w, 46 00:04:05,500 --> 00:04:11,430 I can do the same things that we would do for writing to a file. 47 00:04:11,430 --> 00:04:16,410 So, for example, I might say for number in range of 10, 48 00:04:16,410 --> 00:04:20,950 md dot write of 49 00:04:20,950 --> 00:04:27,880 str of num and md dot write, 50 00:04:27,880 --> 00:04:33,000 a new line character. 51 00:04:33,000 --> 00:04:36,305 Again, the close happens automatically for us. 52 00:04:36,305 --> 00:04:44,055 So, when I run this, and unfortunately, 53 00:04:44,055 --> 00:04:46,490 I'm not seeing the file contents and that reminds 54 00:04:46,490 --> 00:04:49,510 me of one little got you that you might want to remember. 55 00:04:49,510 --> 00:04:51,970 If you're trying to write a file, 56 00:04:51,970 --> 00:04:57,725 you can't write to a file that's the same name as one of the built-in files in the page. 57 00:04:57,725 --> 00:05:01,600 We have a little protective measure that keeps you from overwriting it. 58 00:05:01,600 --> 00:05:04,270 So, if I just change the name of the file, 59 00:05:04,270 --> 00:05:07,900 now I get it as my output, what I was expecting. 60 00:05:07,910 --> 00:05:13,460 You can survive a long time as a Python programmer not using the with construct. 61 00:05:13,460 --> 00:05:15,960 But experienced programmers will use it. 62 00:05:15,960 --> 00:05:18,650 You'll see it on stack overflow and other help sites. 63 00:05:18,650 --> 00:05:22,700 So, it's a good idea to be able to read it and feel free to use it if you like. 64 00:05:22,700 --> 00:05:26,060 See you next time.