1 00:00:00,390 --> 00:00:03,340 Now, let us understand how the program works. 2 00:00:04,020 --> 00:00:08,670 Now here we will, first of all, see working with the files in Python. 3 00:00:09,060 --> 00:00:16,050 Now, before we dive into the code for our file, that is Demo 17 or Beevor, we will first of all examine 4 00:00:16,050 --> 00:00:18,450 how Python works with files. 5 00:00:18,840 --> 00:00:25,830 So the three steps to reading the content of your file are Faustus opening the file, then reading the 6 00:00:25,830 --> 00:00:31,050 content or the file content into a normal variable and finally closing the file. 7 00:00:31,410 --> 00:00:34,440 So similarly, to write a new content in a file. 8 00:00:34,800 --> 00:00:40,420 We must open or create the file, write the new content and again, close device. 9 00:00:40,830 --> 00:00:49,320 So coming to opening of Right Now in Python, we can open a file to read from our right to using the 10 00:00:49,320 --> 00:00:50,210 open function. 11 00:00:50,730 --> 00:00:51,720 The open function. 12 00:00:51,960 --> 00:00:55,070 First parameter is the name of the file to open. 13 00:00:55,530 --> 00:01:02,190 If it is the file in the same folder as the current Python program that you're typing, then you just 14 00:01:02,190 --> 00:01:05,940 give the file named in the command to open the file. 15 00:01:05,940 --> 00:01:12,060 If it existed in the same folder as your Python folder would basically look like if you give, like 16 00:01:12,060 --> 00:01:14,130 here, the file object. 17 00:01:15,020 --> 00:01:24,760 And we are giving your defined name and then we seeing the final object, SDR is equal to open and now 18 00:01:25,130 --> 00:01:32,330 a final object is basically stored in a file object variable, which will be used to read from or write 19 00:01:32,330 --> 00:01:33,140 to the fight. 20 00:01:33,530 --> 00:01:41,030 You can also specify the absolute path of a particular file, which includes the folder and the parent 21 00:01:41,030 --> 00:01:43,100 folders that the file is in. 22 00:01:43,340 --> 00:01:50,960 For example, if it is something like, say, in E drive user folder, slash, python slash ABCDE or 23 00:01:51,800 --> 00:01:56,230 so, you'll have to give this equal number slash user's double slash. 24 00:01:56,240 --> 00:02:01,430 Then between the West and the file name DOT, the 60 is actually your absolute back. 25 00:02:01,760 --> 00:02:07,940 So remember that on Windows the backslash must be escaped by typing another backslash before it. 26 00:02:08,430 --> 00:02:15,080 Now suppose all you want to pass the part of the file as a string for the open functions. 27 00:02:15,080 --> 00:02:22,110 First parameter, then you can directly see or instead of giving the object name, he can give open 28 00:02:22,110 --> 00:02:24,260 in bracket the part of the fight. 29 00:02:24,740 --> 00:02:32,030 The file object has several methods for reading to or writing to, reading from and closing the file. 30 00:02:32,420 --> 00:02:38,360 So coming to the writing to end the closing of files, not for the encryption program. 31 00:02:38,360 --> 00:02:46,190 After reading the text file content, you will need to write the encrypted or decrypted content to a 32 00:02:46,190 --> 00:02:50,470 new file, which you would be doing using the right method. 33 00:02:50,840 --> 00:02:57,440 So to use the right method on the file object, you need to open the file object in the right mood, 34 00:02:57,950 --> 00:03:05,040 which you do by passing open the string W as the second parameter like we have done over here. 35 00:03:05,690 --> 00:03:11,630 Now this second parameter of the argument is an optional parameter because open function can still be 36 00:03:11,630 --> 00:03:13,850 used without passing two arguments. 37 00:03:14,400 --> 00:03:21,410 Now, like here, we have given w this creates a file named the output file name a star which is having 38 00:03:21,410 --> 00:03:26,440 defined them for the output file is in the right mode so you can edit it. 39 00:03:26,750 --> 00:03:32,990 So if a file of the same name exist where the open function creates a new file, the old file would 40 00:03:32,990 --> 00:03:34,150 be overridden. 41 00:03:34,460 --> 00:03:41,810 So be careful when using the open in the right now using the particular file. 42 00:03:42,080 --> 00:03:49,250 Now in the opening right, you can write to the file by calling the right material and the right method 43 00:03:49,340 --> 00:03:56,120 takes one argument, a string of text which you want to write to the file so you can enter something 44 00:03:56,120 --> 00:03:57,530 in the interactive shell. 45 00:03:57,740 --> 00:03:58,670 For example. 46 00:03:58,910 --> 00:04:06,170 C we go back here and the fourth thing that we do here is we will open the file. 47 00:04:06,270 --> 00:04:14,210 So let's take the file object SDR that's equal to the open one file. 48 00:04:14,570 --> 00:04:17,300 See A, B, C, dot the. 49 00:04:20,050 --> 00:04:26,650 OK, now we have to first create this file, so what we'll have to do is we'll have to go to this folder 50 00:04:26,950 --> 00:04:33,190 and let us create your file, see ABC be extinct. 51 00:04:33,640 --> 00:04:38,680 And now here we will see File Obejas Star Dot Open. 52 00:04:39,100 --> 00:04:44,140 We will give your ABC and we want to open it in writable. 53 00:04:45,180 --> 00:04:54,390 So rather, we will say, is equal to all, the first thing is we would give them the open command that 54 00:04:54,390 --> 00:04:58,160 is our final object name is equal to open. 55 00:04:58,650 --> 00:05:07,740 And now once we have open the particular file, we say file object is our DOT Veals right method we 56 00:05:07,740 --> 00:05:11,220 bring to your say hello, Baynton. 57 00:05:11,850 --> 00:05:13,890 File object. 58 00:05:16,240 --> 00:05:25,940 Repressed and now suppose you go back and try to open ABC, we have not yet closed the particular fight. 59 00:05:26,200 --> 00:05:35,940 So what we will do is file object, STF dot close first and then we will open ABC. 60 00:05:35,950 --> 00:05:36,520 You get there. 61 00:05:36,530 --> 00:05:38,560 Hello, Biton file object. 62 00:05:38,560 --> 00:05:45,420 Prenter So when you pass the string to the right matau it right that string to the text file and prints 63 00:05:45,430 --> 00:05:46,460 a particular number. 64 00:05:46,480 --> 00:05:51,820 That is a number of characters in the string return to the file and when you're finished working with 65 00:05:51,820 --> 00:05:58,210 the file you need to tell Biton that it is done with the by calling the function clause method on the 66 00:05:58,210 --> 00:05:59,690 file object as we have done. 67 00:06:00,070 --> 00:06:07,060 So there is also an update on the mode, which is like write more except append mode doesn't override 68 00:06:07,060 --> 00:06:07,550 the file. 69 00:06:07,750 --> 00:06:12,850 So instead strings are written to the end of the content already in the file. 70 00:06:13,150 --> 00:06:18,670 Although we don't use this in this particular program, you can open a file in the append would also 71 00:06:18,670 --> 00:06:23,290 by passing a string E as the second argument to the open function. 72 00:06:23,640 --> 00:06:30,570 So if you get some error like I ordered unsupported operation or not readable error message, then when 73 00:06:30,610 --> 00:06:35,770 you're trying to call the write on a file object, you might not have open the file in the right more. 74 00:06:36,250 --> 00:06:42,100 So when you don't include the open functions optional parameter, it automatically opens the file in 75 00:06:42,100 --> 00:06:49,990 read mode that is are instead, which allows you to only use the reading matter on the file object not 76 00:06:49,990 --> 00:06:50,610 writing. 77 00:06:50,920 --> 00:06:58,150 So let us now see reading the data from the file instead of going in the file directly over here from 78 00:06:58,150 --> 00:07:04,930 Python, we can use the reading matter, which returns a string containing all the text in the fight. 79 00:07:05,410 --> 00:07:13,600 Not to try out that we will read the same ABC file which we have created earlier with the right method. 80 00:07:13,840 --> 00:07:19,480 So now for that we will take the file object as the R is equal to again. 81 00:07:19,480 --> 00:07:22,870 We would give open A, B, C, dot the city. 82 00:07:23,110 --> 00:07:27,610 Now we are giving us even if we don't give out by default, it is in the readable. 83 00:07:27,940 --> 00:07:30,520 But still we are specifying that we want to give out. 84 00:07:30,870 --> 00:07:39,340 Then we give your content as the R is equal to we give file object SDR don't use the read method and 85 00:07:39,340 --> 00:07:41,430 store the values in the given variables. 86 00:07:41,830 --> 00:07:49,210 Then we say print the value of content SDR here, which would print whatever string you have in the 87 00:07:49,220 --> 00:07:50,140 particular fight. 88 00:07:50,440 --> 00:07:55,990 And once you have done, don't forget to again close the file object. 89 00:07:56,200 --> 00:08:02,850 So your file is open and the file object that is created is stored in the file object SDR variable. 90 00:08:03,130 --> 00:08:08,800 So once you have the file object with you, you can read the file using the agreed method and stored 91 00:08:08,800 --> 00:08:09,250 in it. 92 00:08:09,520 --> 00:08:14,890 The content is still variable, which you then print for printing the values in the file. 93 00:08:15,580 --> 00:08:22,690 Now if you get some error messages like IO error or error number two or no such file or directories, 94 00:08:22,690 --> 00:08:26,980 then then make sure that the file actually is where you think it is. 95 00:08:27,160 --> 00:08:33,490 And double click or double check that name that you have typed on the folder name also, or if it is 96 00:08:33,490 --> 00:08:38,100 in a proper direct result of the directories, another vote or a particular folder. 97 00:08:38,530 --> 00:08:45,940 Now we would be using this open read right close on our file that we have opened to encrypt or decrypt 98 00:08:45,940 --> 00:08:48,550 in our demo that we have created. 99 00:08:48,850 --> 00:08:55,470 So now let's go back to our Demo 17, 1735, and let us see setting up our main function. 100 00:08:55,720 --> 00:08:58,860 Now, the first part of this program should look familiar. 101 00:08:59,410 --> 00:09:02,950 We have an import statement which is importing our data. 102 00:09:02,950 --> 00:09:04,660 That is the time object. 103 00:09:04,660 --> 00:09:09,100 If we look at the full statement here, then we are importing the OS. 104 00:09:09,340 --> 00:09:13,330 This is the demo tape and the demo 13 parameters. 105 00:09:13,720 --> 00:09:21,130 Now, then we start with other men by setting up some variables to use in our program. 106 00:09:21,520 --> 00:09:23,320 That is our input file. 107 00:09:23,440 --> 00:09:32,920 Name a star variable which holds a string of a file to read and the encrypted or file or the decrypted 108 00:09:32,920 --> 00:09:37,060 text is returned to the file name in the output file name is still.