Difference between revisions of "Python/C3/Getting-started-with-files/English-timed"

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

Revision as of 14:29, 10 July 2014

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,
  1. Open a file.
  2. Read the contents of the file line by line.
  3. Read the entire content of file at once.
  4. Append the lines of a file to a list.
  5. Close the file.
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!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha