1 00:00:00,150 --> 00:00:06,510 Hello welcome to this tutorial on built in functions in this tutorial. 2 00:00:06,510 --> 00:00:16,350 We will talk about the built in functions file input output print function input function. 3 00:00:16,350 --> 00:00:24,740 These built in functions are very important to know and will be used frequently while coding. 4 00:00:24,760 --> 00:00:28,950 Let's begin with the file input output function. 5 00:00:28,950 --> 00:00:35,900 Python has built and functions to create and manipulate files. 6 00:00:35,940 --> 00:00:47,270 Consider this function the open function this function returns a file object so to the open function 7 00:00:47,570 --> 00:00:52,630 you need to parse the file name as an argument. 8 00:00:53,610 --> 00:00:59,010 All other arguments are optional and have default values. 9 00:00:59,040 --> 00:01:07,680 Say for example if the file that you want to open is in the current working directory you can just provide 10 00:01:07,680 --> 00:01:12,490 the file name like this test dot the X D. 11 00:01:12,570 --> 00:01:20,740 If the file resides in a directory other than that you have to provide the full part with the file name. 12 00:01:20,790 --> 00:01:25,440 Make sure the file name and the PI given is correct. 13 00:01:25,440 --> 00:01:36,380 Otherwise we will get a file not found error coming to the access mode the access mode defined in which 14 00:01:36,380 --> 00:01:45,350 way we want to open a file if we want to open a file for read only write only or both say for example 15 00:01:45,350 --> 00:01:48,420 if we want to open the file for reading. 16 00:01:48,500 --> 00:01:52,660 In that case the access mode would be art. 17 00:01:53,000 --> 00:02:02,410 And this is the default mode if you want to open the file for editing that is for writing will be the 18 00:02:02,410 --> 00:02:04,260 access mode would be w. 19 00:02:05,020 --> 00:02:15,320 And if you want to add text to an existing file you can use the access mode it append. 20 00:02:15,710 --> 00:02:21,860 So I want to create a fly test dot the equity in the current directory. 21 00:02:21,980 --> 00:02:24,490 So I'm going to use this function. 22 00:02:24,560 --> 00:02:30,610 The open function if the file does not exist. 23 00:02:30,610 --> 00:02:42,400 This function would create a new Y in the right more we have to be careful with the access more w as 24 00:02:42,400 --> 00:02:46,930 it will overwrite into the file if it already exists. 25 00:02:47,020 --> 00:02:50,320 All previous data would be erased. 26 00:02:50,320 --> 00:02:57,770 So using the open function I'm going to create the file test data. 27 00:02:57,830 --> 00:03:02,780 Dot the extremes in the right mode. 28 00:03:02,830 --> 00:03:07,600 If the file you that you're going to create is in the current working directory you can just provide 29 00:03:07,600 --> 00:03:10,860 the file name like I did here. 30 00:03:12,260 --> 00:03:19,790 Or if the file decides in a directory other than that you have to provide the full part PDF file name 31 00:03:20,220 --> 00:03:21,590 using the open function. 32 00:03:21,590 --> 00:03:23,350 I'm going to create this file. 33 00:03:23,750 --> 00:03:26,980 So let execute this line of code. 34 00:03:27,050 --> 00:03:27,770 Okay. 35 00:03:27,970 --> 00:03:34,070 Next and this is the file that God created test data the 60. 36 00:03:34,900 --> 00:03:40,030 So we haven't written anything in this file yet so it is empty. 37 00:03:40,030 --> 00:03:47,830 Now let's close this and write into some text into this file and for that you can use the right method 38 00:03:47,860 --> 00:03:51,090 on the file object that we have here. 39 00:03:51,130 --> 00:04:02,110 So in order to do that on this object if you need to call the right method so let's call the right method 40 00:04:03,040 --> 00:04:06,400 and you can enter some texture. 41 00:04:06,730 --> 00:04:19,420 So I'm going to enter first line backslash in the backslash character can be used to tell the interpreter 42 00:04:19,750 --> 00:04:25,350 that the next character following this slash should be treated as a new line. 43 00:04:25,360 --> 00:04:37,490 Now let's and third a few more lines into this file if the dot right let's say second line and then 44 00:04:37,610 --> 00:04:39,120 one more line. 45 00:04:39,140 --> 00:04:44,930 All right so I have endured three lines in this fight. 46 00:04:45,290 --> 00:04:47,440 Let's close the file now. 47 00:04:47,570 --> 00:04:56,710 The close method helps to free up the resources that were tied to defining let us now open the file 48 00:04:56,710 --> 00:04:57,640 again. 49 00:04:57,640 --> 00:05:07,360 Here is the text that we have endured using the right method on the file object. 50 00:05:07,360 --> 00:05:14,280 Now let's add in more text to this file using the append method. 51 00:05:15,350 --> 00:05:18,330 The append method does not or what. 52 00:05:18,350 --> 00:05:23,570 The existing text in the file but keeps adding text to define it. 53 00:05:24,320 --> 00:05:31,710 So let's close this and open the file again since it is closed. 54 00:05:31,710 --> 00:05:35,880 Let's open the file again using the open method. 55 00:05:36,920 --> 00:05:41,420 So the file name is in court. 56 00:05:41,500 --> 00:05:53,340 That file name is test underscore data dot the 60 left open in the append mode. 57 00:05:53,440 --> 00:05:59,620 So we have opened the file in the access mode append. 58 00:05:59,840 --> 00:06:04,570 Now in order to write into the file we are going to use the right method. 59 00:06:04,950 --> 00:06:12,870 I am going to enter this text into this file and then finally I'm going to close define. 60 00:06:13,460 --> 00:06:16,600 After we're done let's open the file again. 61 00:06:18,660 --> 00:06:25,720 The last line has been appended using the access mode a next. 62 00:06:25,770 --> 00:06:37,890 You can also read what is there in a file using the access mode od which is read letters open this file 63 00:06:38,890 --> 00:06:40,610 in the read mode. 64 00:06:40,990 --> 00:06:51,220 So the file name is test data that the X T and then the access mode is odd. 65 00:06:51,240 --> 00:06:58,190 Now say for example I want to read the first four lines in the file. 66 00:06:59,210 --> 00:07:04,490 So I'm going to pass forward as an argument to this method read 67 00:07:07,780 --> 00:07:13,570 so as you can see the first four characters in the file has been retrieved. 68 00:07:13,600 --> 00:07:20,140 Now let's pass forward as an argument again to the Reed method. 69 00:07:20,140 --> 00:07:29,360 The next four characters including these spaces has been retrieved again call the deed method. 70 00:07:29,880 --> 00:07:31,810 The argument forward. 71 00:07:31,900 --> 00:07:39,730 Now let us not pass any argument to the deed method execute this line of code. 72 00:07:40,600 --> 00:07:49,180 So if we do not pass any argument to the deed method it reads all of the remaining text in the file 73 00:07:49,600 --> 00:07:53,010 from that the cursor is position. 74 00:07:53,500 --> 00:08:01,360 So this particular state Moon has a red beginning from where the court said his position to the end 75 00:08:01,360 --> 00:08:04,160 of the file if dot read. 76 00:08:04,190 --> 00:08:13,090 Now again if you try to read the file again it returns an empty string because the cursor does now at 77 00:08:13,090 --> 00:08:14,640 the end of the file. 78 00:08:14,650 --> 00:08:26,230 Now you can use the seek method FDR to seek to bring the cursor back to the 0 8 position so we can use 79 00:08:26,230 --> 00:08:35,040 this method to bring the closer to the specified position so I past 0. 80 00:08:35,040 --> 00:08:43,050 Now let's read the file again by passing the argument forward. 81 00:08:43,200 --> 00:08:46,980 It's gonna be the first four characters. 82 00:08:47,040 --> 00:08:57,990 Now if you want to know the position of the cursor you can use the tail method on the file and it shows 83 00:08:57,990 --> 00:09:02,530 that it is a D fourth position similar to the deed method. 84 00:09:02,550 --> 00:09:08,140 We have the read line method which added individual lines of a file. 85 00:09:08,250 --> 00:09:18,900 Let's call the read line method on the file object so it has read the first line from there D. 86 00:09:19,210 --> 00:09:27,020 Because it position to the end of the line that is the first line let's call the read line method again. 87 00:09:27,110 --> 00:09:37,020 So the entire second line is read so the second line is read from the initial to the ending position 88 00:09:37,670 --> 00:09:38,790 next. 89 00:09:38,880 --> 00:09:50,110 The next method is d e.g. lines method and this method returns a list of remaining lines of the entire 90 00:09:50,210 --> 00:09:51,540 file. 91 00:09:51,550 --> 00:09:56,660 So this is going to do it on a list of lines. 92 00:09:56,690 --> 00:10:05,510 So this is the third line which is one item and this is the fourth line which is the second item in 93 00:10:05,510 --> 00:10:06,380 the list. 94 00:10:06,380 --> 00:10:12,740 And then you can use a for loop to read the file line by line. 95 00:10:12,740 --> 00:10:17,900 And here is the code for reading each line from the file. 96 00:10:17,930 --> 00:10:24,970 The for loop is going to retrieve each line from the fine print the line. 97 00:10:25,070 --> 00:10:33,910 And finally after the for loop breaches of the file we are going to close the file using the close method. 98 00:10:34,070 --> 00:10:37,000 So let's execute this piece of code. 99 00:10:38,760 --> 00:10:45,070 This is how you can use a for loop in order to access each line in a file.