1 00:00:00,470 --> 00:00:02,780 File, read and write operations. 2 00:00:04,070 --> 00:00:13,420 So what is a data file file is a logical unit of related information stored on a Nimda location on disk, 3 00:00:13,850 --> 00:00:20,990 it is used to permanently stored data in a non-volatile memory as example, hard disk. 4 00:00:21,380 --> 00:00:24,170 We can keep our data in different genotypes. 5 00:00:24,320 --> 00:00:30,710 So like the numbers, like your tuples, lists, sits, dictionaries and so on. 6 00:00:31,160 --> 00:00:36,070 But all of them are occupying space in the memory that is REM. 7 00:00:36,350 --> 00:00:41,510 So when the program will get terminated, when my machine will be shut down, in that case, that data 8 00:00:41,510 --> 00:00:42,240 will be lost. 9 00:00:42,590 --> 00:00:49,730 So we are supposed to have some mechanism with the help which we can store our data on to our secondary 10 00:00:49,730 --> 00:00:50,290 storage. 11 00:00:50,450 --> 00:00:52,160 That is a nonvolatile storage. 12 00:00:52,310 --> 00:00:53,410 Let it be hard disk. 13 00:00:53,720 --> 00:00:57,830 So the best means to do that one is through our data files. 14 00:00:58,130 --> 00:01:05,690 So filing is a logical unit of related information stored on a location, on a disk, and it is used 15 00:01:05,690 --> 00:01:10,080 to permanently store data onto a non-volatile memory. 16 00:01:10,280 --> 00:01:12,890 Here it is, our disk as an example. 17 00:01:13,430 --> 00:01:20,270 So since random access memory that is REM is volatile, so it cannot be treated for permanent storage 18 00:01:20,270 --> 00:01:21,130 of data. 19 00:01:21,710 --> 00:01:27,140 When you want to read from or write to a file, we need to open it at first. 20 00:01:27,590 --> 00:01:29,390 Then we are we are done. 21 00:01:29,600 --> 00:01:36,890 And when this one is done, then it needs to be closed so that the resources that are tied with the 22 00:01:36,890 --> 00:01:38,580 file are getting free. 23 00:01:39,080 --> 00:01:45,590 So before reading or writing on the data file, at first we are supposed to open the file in the respective 24 00:01:45,590 --> 00:01:45,950 mode. 25 00:01:46,130 --> 00:01:52,370 Then what about the operations we are supposed to do and get it done and then we shall close the file 26 00:01:52,370 --> 00:01:57,910 so that the allocated resources will be declared as a free so. 27 00:01:57,920 --> 00:02:05,390 Hence, in Python, a file operation takes place in the following sequence that is open a file, read 28 00:02:05,390 --> 00:02:10,050 or write that is a from the respective operation and then close the file. 29 00:02:10,070 --> 00:02:14,870 So there are three steps with the help of which the file handling can be done in Python. 30 00:02:16,550 --> 00:02:23,900 So these are the file python file, Modisane, the first one is are open the file for the reading that 31 00:02:23,900 --> 00:02:27,230 is a by default if we don't mention it will get opened in the army. 32 00:02:27,710 --> 00:02:34,820 That is a read what w open the file for writing creates a new file if it does not exist or truncated 33 00:02:34,820 --> 00:02:36,650 the file if it is existing. 34 00:02:37,070 --> 00:02:38,720 Next one is open. 35 00:02:38,720 --> 00:02:42,680 A file for exclusive creation in the file already exists. 36 00:02:43,070 --> 00:02:44,510 The operation files. 37 00:02:44,540 --> 00:02:45,640 So then is it XML? 38 00:02:45,980 --> 00:02:52,970 In case of a mode open a file that is for appending at the end of the file and without truncating it 39 00:02:53,180 --> 00:02:56,780 creates a new file if it does not exist. 40 00:02:57,050 --> 00:03:04,100 So that is in case of the file is pre existing and then the pre existing file version will get opened 41 00:03:04,310 --> 00:03:06,530 and the new content will be appended back. 42 00:03:06,710 --> 00:03:12,980 If the file is not pre existing then against that, giving them the file get created and that will be 43 00:03:12,980 --> 00:03:13,450 opened. 44 00:03:13,940 --> 00:03:22,070 Next one is to open the file in the text won't be open the file in the binary mode and means open a 45 00:03:22,070 --> 00:03:25,690 file for updating that is reading and writing mode. 46 00:03:25,910 --> 00:03:27,970 So let us go through some examples here. 47 00:03:28,430 --> 00:03:35,330 So if one is going to open employee debt here to see we didn't mention any more, so by default this 48 00:03:35,330 --> 00:03:36,860 armored will be there. 49 00:03:37,040 --> 00:03:42,320 So equivalent to r r r t, what is out there is a read Dixmoor. 50 00:03:42,320 --> 00:03:48,920 You can find this one this r t if two is going to open Pigott GPG R plus B. 51 00:03:49,190 --> 00:03:52,180 So what is the R R stands for this. 52 00:03:52,460 --> 00:03:53,990 This is my read operation. 53 00:03:54,230 --> 00:03:59,870 And what is A B B is that is a binary mod that is read and write in the binary mod. 54 00:04:00,260 --> 00:04:02,330 Next one is the F three that is open. 55 00:04:02,330 --> 00:04:07,580 There is a text in W mode that is a right in the text mod. 56 00:04:07,760 --> 00:04:14,600 So in this way we have given some sample codes to show you that how this mould's can be used while opening 57 00:04:14,600 --> 00:04:15,200 one file. 58 00:04:16,700 --> 00:04:24,440 Closing a file in Biton, so you see, when we are done with the operations to the file, we need to 59 00:04:24,440 --> 00:04:27,740 properly close the file and closing is a file. 60 00:04:27,860 --> 00:04:36,050 Closing a file will free up the resources that were tight with the file as this is done using Python 61 00:04:36,050 --> 00:04:36,970 closed method. 62 00:04:37,220 --> 00:04:42,950 So using this closed method, the file will be closed and the allocated memory space which was allocated 63 00:04:42,950 --> 00:04:45,860 for these files, will be declared as a free space. 64 00:04:46,280 --> 00:04:54,260 Python has a garbage collector to clean up the unrefuted unreferenced objects, but we must not rely 65 00:04:54,290 --> 00:04:56,610 on it to close the file. 66 00:04:56,660 --> 00:05:01,150 So that's why we should close the file ourselves in the coding. 67 00:05:01,160 --> 00:05:07,760 We should not consider the garbage collector will come and it will make all the unreferenced resources 68 00:05:07,910 --> 00:05:08,810 to make them free. 69 00:05:08,840 --> 00:05:09,920 We should not rely on that. 70 00:05:10,220 --> 00:05:11,480 It is our common practice. 71 00:05:11,480 --> 00:05:13,480 It is a good practice for the programmers. 72 00:05:13,640 --> 00:05:16,670 Open the file, do the operations and close it. 73 00:05:17,450 --> 00:05:19,270 So here we are having some examples. 74 00:05:19,280 --> 00:05:26,990 So if it is going to open employed or that encoding is a goal to UTF eight and then perform some operations 75 00:05:26,990 --> 00:05:32,870 file operations, and then if that close to close the file so that they are allocated, resources will 76 00:05:32,870 --> 00:05:34,400 be declared as free. 77 00:05:34,730 --> 00:05:38,510 So in this particular video, we have discussed that how to perform the region. 78 00:05:38,510 --> 00:05:38,750 Right. 79 00:05:38,780 --> 00:05:41,780 Operations on file in case of Python. 80 00:05:42,780 --> 00:05:43,950 That's what watching this video.