1 00:00:00 --> 00:00:07 Hello Friends and Welcome to the tutorial on "Getting started with files". 2 00:00:08 --> 00:00:23 At the end of this tutorial, you will be able to, Open a file. Read the contents of the file line by line. Read the entire content of file at once. Append the lines of a file to a list. Close the file. 3 00:00:24 --> 00:00:33 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists" and "Getting started with For". 4 00:00:34 --> 00:00:36 So now, open the terminal and start ipython 5 00:00:37 --> 00:00:45 So type ipython space hyphen pylab. 6 00:00:46 --> 00:00:53 Let us first open the file, pendulum dot txt present in slash home slash fossee slash . 7 00:00:54 --> 00:01:10 So type f is equal to open within brackets and single quotes slash home slash fossee slash pendulum dot txt. 8 00:01:11 --> 00:01:13 Here f is called a file object. 9 00:01:14 --> 00:01:16 Let us type f on the terminal to see what it is. 10 00:01:17 --> 00:01:21 So type f and hit Enter. 11 00:01:22 --> 00:01:26 The file object shows the filepath and mode of the file which is open. 12 00:01:27 --> 00:01:31 'r' stand for read only mode and 'w' stands for write mode. 13 00:01:32 --> 00:01:39 As you can see, this file is open in read only mode. 14 00:01:40 --> 00:01:46 We shall first learn to read the whole file into a single variable. 15 00:01:47 --> 00:01:52 We use the read method to read all the contents of the file into the variable,pend. 16 00:01:53 --> 00:02:01 So type pend is equal to f dot read closing brackets and hit Enter. 17 00:02:02 --> 00:02:10 Now, let us see what pend contains, by typing Print space pend 18 00:02:11 --> 00:02:14 We can see that pend has all the data of the file. 19 00:02:15 --> 00:02:24 Type just pend to see more explicitly, what it contains. 20 00:02:25 --> 00:02:29 So now, Pause the video here, try out the following exercise and resume the video. 21 00:02:30 --> 00:02:39 Split the variable into a list, pend underscore list, of the lines in the file. 22 00:02:40 --> 00:02:43 We use the function split lines to solve this problem. 23 00:02:44 --> 00:03:04 So type pend underscore list is equal to pend dot split lines closing brackets and hit Enter. 24 00:03:05 --> 00:03:10 Now, let us learn to read the file line-by-line. 25 00:03:11 --> 00:03:18 But, before that we will have to close the file, since the file has already been read till the end. 26 00:03:19 --> 00:03:23 Let us close the file opened into f. 27 00:03:24 --> 00:03:28 Then type f dot close closing brackets and hit Enter. 28 00:03:29 --> 00:03:36 Again type f on the prompt to see what it contains. 29 00:03:37 --> 00:03:41 Notice, that it now says the file has been closed. 30 00:03:42 --> 00:03:49 It is a good programming practice to close any file objects that we have opened, after their job is done. 31 00:03:50 --> 00:03:53 Let us, now move on to reading files line-by-line. 32 00:03:54 --> 00:03:59 Pause the video here, try out the following exercise and resume the video. 33 00:04:00 --> 00:04:04 Re-open the file pendulum dot txt with f as the file object. 34 00:04:05 --> 00:04:17 We just use the up arrow until we reach the open command and issue it again.Then hit Enter. 35 00:04:18 --> 00:04:26 Now, to read the file line-by-line, we iterate over the file object line-by-line, using the for command. 36 00:04:27 --> 00:04:34 Let us iterate over the file line-wise and print each of the lines. 37 00:04:35 --> 00:04:46 So type in the command for space line space in space f colon , then , print line. 38 00:04:47 --> 00:04:52 line is a variable, sometimes called the loop variable, and it is not a keyword. 39 00:04:53 --> 00:04:59 We could have used any other variable name, but line seems meaningful enough. 40 00:05:00 --> 00:05:06 Instead of just printing the lines, let us append them to a list, line underscore list . 41 00:05:07 --> 00:05:11 We first initialize an empty list, line underscore list. 42 00:05:12 --> 00:05:21 for that type line underscore list is equal to square bracket and hit Enter. 43 00:05:22 --> 00:05:29 Let us then read the file line-by-line and then append each of the lines to the list. 44 00:05:30 --> 00:05:35 We could, as usual close the file using f.close and re-open it. 45 00:05:36 --> 00:05:42 But, this time, let's leave alone the file object f and directly open the file within the for statement. 46 00:05:43 --> 00:05:48 This will save us the trouble of closing the file, each time we open it. 47 00:05:49 --> 00:06:21 So type for line in open within brackets and single quotes slash home slash fossee slash pendulum dot txt colon line underscore list dot append within brackets line,Hit Enter. 48 00:06:22 --> 00:06:25 Let us see what line underscore list contains. 49 00:06:26 --> 00:06:32 so type line underscore list and hit Enter. 50 00:06:33 --> 00:06:41 Notice that line_list is a list of the lines in the file, along with the newline characters. 51 00:06:42 --> 00:06:51 If you noticed, pend underscore list did not contain the newline characters, because the string pend was split on the newline characters. 52 00:06:52 --> 00:07:03 We can strip out the newline characters from the lines by using some string methods which we shall look in the further tutorial on strings. 53 00:07:04 --> 00:07:11 So now, This brings us to the end of this tutorial. Lets revise what we have learnt, 54 00:07:12 --> 00:07:16 1. Open and close files using the open and close functions respectively. 55 00:07:17 --> 00:07:21 2. Read the data in the files as a whole,by using the read function. 56 00:07:22 --> 00:07:30 3. Read the data in the files line by line by iterating over the file object using the for loop. 57 00:07:31 --> 00:07:37 and finally Append the lines of a file to a list using the append function within the for loop. 58 00:07:38 --> 00:07:41 Here are some self assessment questions for you 59 00:07:42 --> 00:07:45 1. The open function returns a 60 00:07:46 --> 00:07:47 string 61 00:07:48 --> 00:07:48 list 62 00:07:49 --> 00:07:49 file object 63 00:07:50 --> 00:07:51 function 64 00:07:52 --> 00:07:56 2. What does the function splitlines() do. 65 00:07:57 --> 00:08:00 Displays the data as strings,all in a line 66 00:08:01 --> 00:08:02 Displays the data line by line as strings 67 00:08:03 --> 00:08:06 Displays the data line by line but not as strings 68 00:08:07 --> 00:08:08 So now,let us look at the answers, 69 00:08:09 --> 00:08:14 1.The function open , returns a file object. 70 00:08:15 --> 00:08:20 2. The function splitlines displays the data line by line as strings. 71 00:08:21 --> 00:08:26 So we hope you have enjoyed this tutorial and found it useful. 72 00:08:27 --> 00:08:32 Thank you!