WEBVTT 00:00.300 --> 00:05.150 Now in this tutorial we will learn about the concept of file handling. 00:05.910 --> 00:10.410 Let's close this and we will create a sample application. 00:10.440 --> 00:15.030 We will create a new python file file. 00:15.520 --> 00:15.950 Right. 00:15.960 --> 00:18.450 Right. 00:18.600 --> 00:21.800 So we have created a new python file. 00:22.260 --> 00:28.480 Let's import the package from the import star. 00:29.490 --> 00:33.980 After that we will use that option. 00:35.070 --> 00:38.680 Now we will use the file right option. 00:38.970 --> 00:47.950 We have created an element W and we will use the inveighed function open and we will use simple start 00:48.260 --> 00:49.290 texting. 00:49.710 --> 00:53.490 And we will use this file as an output file. 00:53.490 --> 00:55.530 Let's create this file here. 00:55.890 --> 01:03.500 Right click new file and we have to give the same name which we have used in that application that is 01:03.500 --> 01:06.260 in sampled RTX. 01:07.260 --> 01:15.670 So we created an plan file by December d'arte the XTi. 01:16.080 --> 01:19.210 Now let's see what values we have. 01:19.210 --> 01:25.000 Firstly open the file semi-dark Dickstein have w is equal to open. 01:25.110 --> 01:28.200 Now I will use R W is a query to 01:32.510 --> 01:37.630 w dark right function inside a function. 01:37.640 --> 01:41.080 Let's write something. 01:41.090 --> 01:43.220 I use that right thing. 01:43.220 --> 01:49.540 Some say stuff in my text file. 01:53.090 --> 01:54.450 TXT file. 01:54.460 --> 01:57.660 Slash and for the next line. 01:57.660 --> 02:01.130 Similarly I really don't. 02:01.770 --> 02:03.080 I will use. 02:03.690 --> 02:06.630 I like Pepsi 02:10.320 --> 02:11.220 Chinese 02:13.740 --> 02:20.680 Mugler noodles slash and fall next line. 02:20.920 --> 02:24.790 So I have writing these two lines in my file. 02:25.020 --> 02:30.120 Now I will use after your dog PLO's option. 02:30.160 --> 02:33.160 Now we have righted the text in the file. 02:33.600 --> 02:36.120 Let's broaden this example. 02:37.280 --> 02:43.340 Give here the name as file read Sauti. 02:43.490 --> 02:45.680 We have to go at that option. 02:45.680 --> 02:55.400 We have not linked dollar by file with this name so we will just select the file read where we'll go 02:55.400 --> 02:57.190 an option off. 02:58.160 --> 03:00.440 Now let's run this. 03:00.440 --> 03:05.820 After running this let's open our semi-dark DXi file here. 03:05.840 --> 03:07.230 Are you able to see there. 03:07.490 --> 03:15.040 We have got two lines written in this blank file that is writing some stuff in my text file. 03:15.350 --> 03:18.450 I like baps the Chinese bug noodles. 03:18.780 --> 03:26.450 Now what we do is I want to display the content of my old wood pile on my console window. 03:27.490 --> 03:33.960 So for that we have to use the read option. 03:34.160 --> 03:41.770 So let's create an Dalmane as far is equal to hoppen. 03:42.030 --> 03:48.090 And here I even gave the name of my file that is dark DXi. 03:48.480 --> 03:56.290 I will open the file and I read more and after I've called either text off that 03:59.610 --> 04:11.970 fireguard read I will copy the text in the text for the label which I have created with the name as 04:11.970 --> 04:13.260 text. 04:13.410 --> 04:16.130 Now I will print this. 04:16.680 --> 04:21.320 I will use the brain function and I will parse the text here. 04:21.840 --> 04:25.750 Now I really use far dark claws option. 04:28.000 --> 04:35.180 Now let's read on that allegation so we do see the chord which is written in Zen by Dorothy Eckstine 04:35.530 --> 04:37.730 with the help of F for LMA. 04:37.900 --> 04:46.600 I have opened the file box and the XTi we have read their text and we bring that text on our console 04:46.900 --> 04:49.630 for it is written there in Zambia or Dickstein. 04:49.660 --> 04:55.940 We are able to view the contents where it is being written in that same Bidar DXP file. 04:56.170 --> 05:02.720 So we have implemented the concept of file handling reading and writing from files. 05:02.770 --> 05:13.600 So now what I wanted to use is I want to show to show the concept of exception handling so lets use 05:13.600 --> 05:16.610 the commands in these commands. 05:16.630 --> 05:17.620 I want to use 05:20.490 --> 05:27.450 I'm going to implement the concept of exception handling. 05:27.890 --> 05:32.020 So let's begin the code for exception handling. 05:32.170 --> 05:36.760 What we will use is we will have a loop here. 05:37.310 --> 05:45.350 While true true we are going to use the type block inside the tribe block. 05:45.480 --> 05:53.380 I will create a variable named as number is off in the jet type. 05:54.350 --> 06:00.020 And I really take the input parameter in that input by make. 06:00.300 --> 06:03.040 I'm going to write this thing. 06:03.420 --> 06:12.570 What does your fabric How is the number. 06:14.240 --> 06:15.960 What is your favorite. 06:15.960 --> 06:19.140 How is the number off today. 06:19.310 --> 06:22.050 I warn the next plane option. 06:22.280 --> 06:25.520 Now what type 1 Do you is. 06:25.940 --> 06:30.310 I will use the print option inside the drink option. 06:30.440 --> 06:33.550 I will use it. 06:33.600 --> 06:37.510 DEAN I will use it. 06:37.540 --> 06:45.070 Dean divided by the number which is being informed by the user. 06:45.480 --> 06:52.270 So we are going to use now we will implement the concept of Greg's statement. 06:52.470 --> 06:55.120 We will break the law here. 06:55.270 --> 07:06.680 Now after they really use accepte option that accept option I really have an option off Valuator inside 07:06.680 --> 07:08.240 this valuator. 07:08.240 --> 07:10.960 I will bring another message. 07:11.450 --> 07:15.760 Name does make sure. 07:16.370 --> 07:20.890 And and the number. 07:20.890 --> 07:27.920 So I'm going to display this message in the value of. 07:28.660 --> 07:36.450 Now what if I want to have another exception name as the zero division thereof. 07:37.010 --> 07:43.170 With this exception Ivon to display a message here. 07:43.910 --> 07:50.610 Don't pick zero I want to display messages. 07:50.670 --> 07:53.190 If a customer. 07:53.270 --> 08:02.340 If your boss son is going to and a number for the application then the numbers should not be zero. 08:02.390 --> 08:06.090 From the options. 08:06.870 --> 08:09.550 Know what they want to use is. 08:09.620 --> 08:20.190 I have accept statements inside that except I will have brake option and I will have a finally block 08:20.640 --> 08:22.250 inside the finally block. 08:22.440 --> 08:30.630 I will print the message name does loop complete. 08:31.920 --> 08:37.060 We will have the message no complete. 08:38.230 --> 08:41.350 Now let's run this application. 08:41.350 --> 08:49.570 The thing is that we have not commanded the above code but noise shows we have gotten to our ports. 08:49.570 --> 08:53.010 One is for file reading and writing that is full. 08:53.050 --> 08:55.170 What is your favorite housing number. 08:55.510 --> 08:58.390 Let's say zero phrasing. 08:59.490 --> 09:02.960 It is saying when the message is don't big 0. 09:03.040 --> 09:03.820 So it does. 09:03.910 --> 09:11.240 And third under this exception 0 or division error and it is again asking me the favorite number. 09:11.290 --> 09:15.410 Let's say I have given here the 6. 09:15.750 --> 09:26.140 See it has work done and it has angered microblog it has asked me what is your favorite housey number. 09:26.440 --> 09:33.160 It has given me a printed message that is being divided by the number being dirt. 09:33.430 --> 09:40.770 So I have angered the 6 option and it has given me a divided by 6. 09:41.230 --> 09:51.670 3 as output and after it has gone to the finally block in the finally block it does bring me a message 09:51.670 --> 09:53.700 of loop complete. 09:54.370 --> 10:04.510 So we have done the implementation of file reading file writing and we have done the implementation. 10:04.720 --> 10:10.560 Try klatsch try catch except here. 10:10.960 --> 10:16.630 I want to show you that particular things in the form of exception handling. 10:16.630 --> 10:20.030 We are not having to catch blocks. 10:20.140 --> 10:30.740 We are having the accepte block after the try block and we can use as many as accept try. 10:31.030 --> 10:38.000 So like in case of Java we have polecats statements after their try. 10:38.290 --> 10:46.120 Similarly in the bite and concept we will have a try block with multiple except blocks and after that 10:46.210 --> 10:53.400 I can use a single finally block which will be executed in any d'Armes and condition. 10:53.410 --> 10:54.950 It is fine though. 10:54.950 --> 10:58.100 Then the finally block will be executed. 10:58.300 --> 10:59.780 So it is a show. 10:59.890 --> 11:02.740 The finally block really definitely execute. 11:02.950 --> 11:10.210 So we have implemented the concept of try except and finally block and we have seen the example of file 11:10.210 --> 11:13.360 handling with the help both read and write functions.