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