1 00:00:00,720 --> 00:00:01,980 Instructor: Welcome back. 2 00:00:01,980 --> 00:00:04,620 One of the common patterns when working with files 3 00:00:04,620 --> 00:00:08,039 is to actually put them in a try accept block. 4 00:00:08,039 --> 00:00:11,700 So in here we can say, try 5 00:00:11,700 --> 00:00:15,840 and actually put them like this 6 00:00:15,840 --> 00:00:19,530 and try out to see if the file exists. 7 00:00:19,530 --> 00:00:23,980 So we can say except and let's say 8 00:00:25,141 --> 00:00:30,003 FileNotFoundError, which we've seen before. 9 00:00:31,560 --> 00:00:34,740 And here I can say as err 10 00:00:34,740 --> 00:00:36,340 and inside we'll say print 11 00:00:37,410 --> 00:00:40,593 file does not exist. 12 00:00:41,490 --> 00:00:43,410 So that now if I run this, 13 00:00:43,410 --> 00:00:46,380 and remember in our current directory 14 00:00:46,380 --> 00:00:48,480 we don't have sad.text. 15 00:00:48,480 --> 00:00:49,683 If I run this, 16 00:00:51,720 --> 00:00:53,910 I get an error file does not exist. 17 00:00:53,910 --> 00:00:56,430 We can even raise this error if we wanted to. 18 00:00:56,430 --> 00:00:58,563 So we can say raise err. 19 00:00:59,460 --> 00:01:01,560 So now it works as expected 20 00:01:01,560 --> 00:01:03,300 I'll get file does not exist 21 00:01:03,300 --> 00:01:05,072 and then give a FileNotFoundError. 22 00:01:06,990 --> 00:01:09,450 So this is a common way to work with files 23 00:01:09,450 --> 00:01:11,700 to handle different errors. 24 00:01:11,700 --> 00:01:16,590 Another common one with files 25 00:01:16,590 --> 00:01:19,000 is this idea of an IOError 26 00:01:20,790 --> 00:01:25,560 and an IOError usually happens when the computer 27 00:01:25,560 --> 00:01:29,340 or the machine you're on has some issue reading or writing 28 00:01:29,340 --> 00:01:32,223 or doing any sort of IO operation. 29 00:01:33,240 --> 00:01:38,240 So you can have something like IO Error in here 30 00:01:38,730 --> 00:01:41,253 and raise that error if you want. 31 00:01:42,630 --> 00:01:45,490 Again, if I run this FileNotFoundError 32 00:01:47,550 --> 00:01:51,790 if for some reason I give it a weird mode to do 33 00:01:53,370 --> 00:01:55,380 I get an unsupported operation error. 34 00:01:55,380 --> 00:01:56,940 The options are endless 35 00:01:56,940 --> 00:02:01,470 but I hope by now you have an idea of how to do file IO. 36 00:02:01,470 --> 00:02:04,680 But let's do a fun exercise because up until now 37 00:02:04,680 --> 00:02:06,630 we haven't really done anything exciting. 38 00:02:06,630 --> 00:02:10,770 We just read a bunch of texts from a file and that was it. 39 00:02:10,770 --> 00:02:13,800 Nothing that creative, right? 40 00:02:13,800 --> 00:02:15,780 So let's do something fun. 41 00:02:15,780 --> 00:02:20,580 In the next video we're gonna try and build a translator. 42 00:02:20,580 --> 00:02:23,010 That's right, I'll see you in the next video. 43 00:02:23,010 --> 00:02:23,843 Bye bye.