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