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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !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 t…')
 
Line 30: Line 30:
 
|-
 
|-
 
|  0:46
 
|  0:46
| Let us first open the file, <tt>pendulum dot txt</tt> present in <tt> slash home slash fossee slash</tt>.
+
| Let us first open the file, pendulum dot txt present in slash home slash fossee slash .
  
 
|-
 
|-
Line 38: Line 38:
 
|-
 
|-
 
|  1:11
 
|  1:11
| Here <tt>f</tt> is called a file object.
+
| Here f is called a file object.
  
 
|-
 
|-
 
| 1:14
 
| 1:14
| Let us type <tt>f</tt> on the terminal to see what it is.
+
| Let us type f on the terminal to see what it is.
  
 
|-
 
|-
Line 66: Line 66:
 
|-
 
|-
 
|1:47
 
|1:47
| We use the <tt>read</tt> method to read all the contents of the file into the variable, <tt>pend</tt>.
+
| We use the read method to read all the contents of the file into the variable,pend.
  
 
|-
 
|-
Line 74: Line 74:
 
|-
 
|-
 
|  2:02
 
|  2:02
| Now, let us see what <tt>pend</tt> contains, by typing Print space pend
+
| Now, let us see what pend contains, by typing Print space pend
  
 
|-
 
|-
 
|2:11
 
|2:11
| We can see that <tt>pend</tt> has all the data of the file.  
+
| We can see that pend has all the data of the file.  
  
 
|-
 
|-
 
|2:15
 
|2:15
|Type just <tt>pend</tt> to see more explicitly, what it contains.
+
|Type just pend to see more explicitly, what it contains.
  
 
|-
 
|-
Line 94: Line 94:
 
|-
 
|-
 
|  2:40
 
|  2:40
| We use the function <tt>splitlines</tt> to solve this problem.
+
| We use the function split lines to solve this problem.
  
 
|-
 
|-
 
|2:44
 
|2:44
|So type pend underscore list is equal to pend dot splitlines closing brackets and hit Enter.
+
|So type pend underscore list is equal to pend dot split lines closing brackets and hit Enter.
  
 
|-
 
|-
Line 118: Line 118:
 
|-
 
|-
 
|  3:29
 
|  3:29
| Again type <tt>f</tt> on the prompt to see what it contains.
+
| Again type f   on the prompt to see what it contains.
  
 
|-
 
|-
Line 138: Line 138:
 
|-
 
|-
 
|4:00
 
|4:00
|Re-open the file <tt>pendulum dot txt</tt> with <tt>f</tt> as the file object.
+
|Re-open the file pendulum dot txt with f as the file object.
  
 
|-
 
|-
Line 146: Line 146:
 
|-
 
|-
 
|  4:18
 
|  4:18
| Now, to read the file line-by-line, we iterate over the file object line-by-line, using the <tt>for</tt> command.  
+
| Now, to read the file line-by-line, we iterate over the file object line-by-line, using the for command.  
  
 
|-
 
|-
Line 158: Line 158:
 
|-
 
|-
 
| 4:47
 
| 4:47
| <tt>line</tt> 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
 
|4:53
|We could have used any other variable name, but <tt>line</tt> seems meaningful enough.
+
|We could have used any other variable name, but line seems meaningful enough.
  
 
|-
 
|-
 
| 5:00
 
| 5:00
| Instead of just printing the lines, let us append them to a list, <tt>line underscore list</tt>.  
+
| Instead of just printing the lines, let us append them to a list, line underscore list .  
  
  
Line 172: Line 172:
 
|-
 
|-
 
|5:07
 
|5:07
|We first initialize an empty list, <tt>line underscore list</tt>.
+
|We first initialize an empty list, line underscore list.
  
 
|-
 
|-
Line 184: Line 184:
 
|-
 
|-
 
|5:30
 
|5:30
|We could, as usual close the file using <tt>f.close</tt> and re-open it.  
+
|We could, as usual close the file using f.close and re-open it.  
  
 
|-
 
|-
 
|5:36
 
|5:36
|But, this time, let's leave alone the file object <tt>f</tt> 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.
  
 
|-
 
|-
Line 203: Line 203:
 
|-
 
|-
 
|  6:22
 
|  6:22
| Let us see what <tt>line underscore list</tt> contains.
+
| Let us see what line underscore list contains.
  
 
|-
 
|-
Line 211: Line 211:
 
|-
 
|-
 
| 6:33
 
| 6:33
| Notice that <tt>line_list</tt> 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
 
| 6:42
| If you noticed, <tt>pend underscore list</tt> did not contain the newline characters, because the string <tt>pend</tt>, 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.
  
 
|-
 
|-
Line 227: Line 227:
 
|-
 
|-
 
|7:12
 
|7:12
|1. Open and close files using the <tt>open</tt> and <tt>close</tt> functions respectively.
+
|1. Open and close files using the open and close functions respectively.
  
 
|-
 
|-
 
|7:17
 
|7:17
|2. Read the data in the files as a whole,by using the <tt>read</tt> function.
+
|2. Read the data in the files as a whole,by using the read function.
  
 
|-
 
|-
 
|7:22
 
|7:22
|3. Read the data in the files line by line by iterating over the file object using the <tt>for</tt> loop.
+
|3. Read the data in the files line by line by iterating over the file object using the for loop.
  
 
|-
 
|-
 
|7:31
 
|7:31
|and finally Append the lines of a file to a list using the <tt>append</tt> function within the <tt>for</tt> loop.
+
|and finally Append the lines of a file to a list using the append function within the for loop.
  
 
|-
 
|-
 
| 7:38
 
| 7:38
| Here are some self assessment questions for you to solve
+
| Here are some self assessment questions for you  
  
 
|-
 
|-
 
|7:42
 
|7:42
|1. The <tt>open</tt> function returns a
+
|1. The open function returns a
  
 
|-
 
|-
Line 267: Line 267:
 
|-
 
|-
 
|7:52
 
|7:52
|2. What does the function <tt>splitlines()</tt> do.
+
|2. What does the function splitlines() do.
  
 
|-
 
|-
Line 287: Line 287:
 
|-
 
|-
 
|8:09
 
|8:09
|1.The function <tt>open</tt>, returns a file object.
+
|1.The function open , returns a file object.
  
 
|-
 
|-
 
|8:15
 
|8:15
|2. The function <tt>splitlines</tt> displays the data line by line as strings.
+
|2. The function splitlines displays the data line by line as strings.
  
  

Revision as of 12:40, 15 March 2013

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,
  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.
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!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha