1 00:00:00,379 --> 00:00:02,890 When it comes to opening files with Python, 2 00:00:02,900 --> 00:00:07,440 remember that we don't always have to use the with statement. 3 00:00:07,929 --> 00:00:11,840 The reason why we use with most of the time is because with, with 4 00:00:12,000 --> 00:00:12,920 statement 5 00:00:13,609 --> 00:00:17,809 automatically the file will be closed once it has been accessed in 6 00:00:18,100 --> 00:00:19,379 this scenario right now, 7 00:00:19,389 --> 00:00:23,700 where we're simply working with the open function to open up the file, 8 00:00:23,879 --> 00:00:27,920 we have to manually close the file by saying file that close, 9 00:00:28,200 --> 00:00:33,000 otherwise the file will remain open and that could lead to potential errors. 10 00:00:33,340 --> 00:00:33,919 So 11 00:00:34,040 --> 00:00:38,009 the question right now is how would we handle for errors in 12 00:00:38,180 --> 00:00:45,119 this particular kind of scenario? Well, we'll have to make use of the finally block. 13 00:00:45,700 --> 00:00:47,200 Now, at the very top, 14 00:00:47,880 --> 00:00:49,319 I'm going to start off with try 15 00:00:49,979 --> 00:00:53,810 as usual. And then remember, of course, everything has to fall on the try. 16 00:00:54,319 --> 00:00:59,029 And now on line five, this is where we're going to add the accept. 17 00:00:59,779 --> 00:01:05,000 And now let's look for the file, the file not found error. 18 00:01:05,709 --> 00:01:06,970 OK? Colon. 19 00:01:07,760 --> 00:01:10,779 So if the file has not been found, 20 00:01:11,080 --> 00:01:14,220 we can simply print file, not found. 21 00:01:14,519 --> 00:01:15,540 However, 22 00:01:15,739 --> 00:01:18,239 what if the file was in fact found 23 00:01:18,379 --> 00:01:19,900 there is no error, 24 00:01:20,099 --> 00:01:23,379 what we're gonna do right now is we're going to say finally, 25 00:01:23,790 --> 00:01:24,540 OK? 26 00:01:25,029 --> 00:01:26,040 And now 27 00:01:26,550 --> 00:01:30,300 if file, hold on. So now we're saying 28 00:01:30,430 --> 00:01:35,459 finally, if there was no error message at all, if the file was in fact found, 29 00:01:35,470 --> 00:01:37,019 what do we now do, 30 00:01:37,379 --> 00:01:39,750 we can simply print out the content. 31 00:01:40,050 --> 00:01:42,690 And now we can also close the file. 32 00:01:42,879 --> 00:01:46,260 And in fact, for good measure, not necessary, but for good measure, 33 00:01:46,269 --> 00:01:47,559 we can just simply print 34 00:01:48,010 --> 00:01:49,760 uh file 35 00:01:50,029 --> 00:01:51,089 closed 36 00:01:51,440 --> 00:01:54,360 just as an example. And of course, if I run the program right now, 37 00:01:54,639 --> 00:01:56,720 you can see it works perfectly fine 38 00:01:56,849 --> 00:02:00,000 and it even says file closed 39 00:02:00,449 --> 00:02:01,779 this program 40 00:02:02,010 --> 00:02:04,360 on the surface right now looks good. 41 00:02:05,120 --> 00:02:08,139 There are no arrows, file was found, 42 00:02:08,360 --> 00:02:09,660 it's printed it out. 43 00:02:09,970 --> 00:02:13,419 And of course, the file has also been closed. 44 00:02:13,610 --> 00:02:16,940 However, we're not done yet 45 00:02:17,610 --> 00:02:18,990 because in fact, 46 00:02:19,369 --> 00:02:23,229 if I was to deliberately change the name of the file 47 00:02:23,600 --> 00:02:25,350 so that we do get the 48 00:02:25,660 --> 00:02:27,759 file, not found error 49 00:02:28,250 --> 00:02:30,130 if I run my program. 50 00:02:30,350 --> 00:02:31,110 Uh oh, 51 00:02:31,399 --> 00:02:32,789 you can see right now 52 00:02:33,169 --> 00:02:35,550 we're still having the usual 53 00:02:35,880 --> 00:02:40,820 uh unpleasant way of displaying or handling our errors. 54 00:02:41,000 --> 00:02:45,350 It's like the try except block of code did not work at all. 55 00:02:45,800 --> 00:02:47,309 What is going on here. 56 00:02:48,429 --> 00:02:50,449 Another thing you need to keep in mind is 57 00:02:50,460 --> 00:02:54,490 that if you're not working with the with statement, 58 00:02:54,809 --> 00:02:58,919 not only would you have to manually close your file, 59 00:02:59,160 --> 00:03:05,289 but you also have to initialize the file at the very beginning 60 00:03:05,639 --> 00:03:07,449 because you see right now as it is, 61 00:03:07,649 --> 00:03:11,029 Python doesn't even really know what file itself is. 62 00:03:11,039 --> 00:03:13,410 Like, what's the default value of file? 63 00:03:14,000 --> 00:03:15,240 A file 64 00:03:15,449 --> 00:03:20,240 isn't equal to uh example that takes like if the file wasn't found, 65 00:03:21,449 --> 00:03:24,119 how would Python know that? OK. 66 00:03:24,190 --> 00:03:28,990 There's actually an error in here like what will be the default value 67 00:03:29,190 --> 00:03:32,309 that the file variable needs to have. 68 00:03:32,839 --> 00:03:34,949 So this is where 69 00:03:35,110 --> 00:03:40,229 we will have to at the very top say file equals none. 70 00:03:41,389 --> 00:03:45,710 Right now, at the very start, we are initializing the file variable, 71 00:03:45,720 --> 00:03:48,729 we're assigning it to the default value of none. 72 00:03:48,740 --> 00:03:50,529 And this is a common practice when dealing with 73 00:03:50,539 --> 00:03:55,000 resources that may or may not be successfully allocated. 74 00:03:55,009 --> 00:03:56,479 So using non here 75 00:03:56,580 --> 00:03:59,729 would allow for a very safe default value that 76 00:03:59,740 --> 00:04:06,089 will prevent any accidental operations on an uninitialized variable. 77 00:04:06,350 --> 00:04:09,809 So this is another thing you have to keep in mind. So right now, 78 00:04:10,119 --> 00:04:13,300 if I run the program one more time, OK, it works well. 79 00:04:13,309 --> 00:04:17,420 If I change the name of the file deliberately to, to produce the error message, 80 00:04:17,500 --> 00:04:20,089 you can see right now it handle the error 81 00:04:20,190 --> 00:04:21,500 gracefully. 82 00:04:21,720 --> 00:04:22,309 So 83 00:04:22,440 --> 00:04:24,140 this is exactly why 84 00:04:24,480 --> 00:04:26,170 when it comes to handling files, 85 00:04:26,179 --> 00:04:29,700 we typically use the with statement because with the with statement, 86 00:04:30,059 --> 00:04:32,959 you don't have to initialize your variables or your files 87 00:04:33,079 --> 00:04:35,279 and you don't have to close them manually. Either 88 00:04:35,519 --> 00:04:38,709 the word statement is so powerful, it will automatically 89 00:04:39,000 --> 00:04:41,769 initialize our files and variables and also close 90 00:04:41,779 --> 00:04:44,609 the file automatically once it has been accessed. 91 00:04:44,619 --> 00:04:45,570 But nevertheless, 92 00:04:45,579 --> 00:04:47,980 you should be aware of this if you find 93 00:04:47,989 --> 00:04:51,309 yourself in a scenario or in a programming code where 94 00:04:51,609 --> 00:04:55,510 the width statement wasn't used. How would you handle errors? 95 00:04:55,519 --> 00:04:57,250 How would you close the file manually? 96 00:04:57,260 --> 00:05:01,089 And of course, how would you also initialize the file manually? 97 00:05:01,100 --> 00:05:02,950 Thank you for watching. I will see you in the next class.