|
1 ======== |
|
2 Script |
|
3 ======== |
|
4 |
|
5 Welcome to the tutorial on getting started with files. |
|
6 |
|
7 {{{ Screen shows welcome slide }}} |
|
8 |
|
9 {{{ Show the outline for this tutorial }}} |
|
10 |
|
11 In this tutorial we shall learn to read files, and do some basic |
|
12 actions on the file, like opening and reading a file, closing a |
|
13 file, iterating through the file line-by-line, and appending the |
|
14 lines of a file to a list. |
|
15 |
|
16 {{{ switch back to the terminal }}} |
|
17 |
|
18 As usual, we start IPython, using |
|
19 :: |
|
20 |
|
21 ipython -pylab |
|
22 |
|
23 Let us first open the file, ``pendulum.txt`` present in |
|
24 ``/home/fossee/``. |
|
25 :: |
|
26 |
|
27 f = open('/home/fossee/pendulum.txt') |
|
28 |
|
29 ``f`` is called a file object. Let us type ``f`` on the terminal to |
|
30 see what it is. |
|
31 :: |
|
32 |
|
33 f |
|
34 |
|
35 The file object shows, the file which is open and the mode (read |
|
36 or write) in which it is open. |
|
37 |
|
38 We shall first learn to read the whole file into a single |
|
39 variable. Later, we shall look at reading it line-by-line. We use |
|
40 the ``read`` method of ``f`` to read, all the contents of the file |
|
41 into the variable ``pend``. |
|
42 :: |
|
43 |
|
44 pend = f.read() |
|
45 |
|
46 Now, let us see what is in ``pend``, by typing |
|
47 :: |
|
48 |
|
49 print pend |
|
50 |
|
51 We can see that ``pend`` has all the data of file. Type just ``pend`` |
|
52 to see more explicitly, what it contains. |
|
53 :: |
|
54 |
|
55 pend |
|
56 |
|
57 %%1%% Pause the video here and split the variable into a list, |
|
58 ``pend_list``, of the lines in the file and then resume the |
|
59 video. Hint, use the tab command to see what methods the string |
|
60 variable has. |
|
61 |
|
62 #[punch: should this even be put? add dependency to strings LO, |
|
63 where we mention that strings have methods for manipulation. hint: |
|
64 use splitlines()] |
|
65 :: |
|
66 |
|
67 pend_list = pend.splitlines() |
|
68 |
|
69 pend_list |
|
70 |
|
71 Now, let us learn to read the file line-by-line. But, before that |
|
72 we will have to close the file, since the file has already been |
|
73 read till the end. |
|
74 #[punch: should we mention file-pointer?] |
|
75 |
|
76 Let us close the file opened into f. |
|
77 :: |
|
78 |
|
79 f.close() |
|
80 |
|
81 Let us again type ``f`` on the prompt to see what it shows. |
|
82 :: |
|
83 |
|
84 f |
|
85 |
|
86 Notice, that it now says the file has been closed. It is a good |
|
87 programming practice to close any file objects that we have |
|
88 opened, after their job is done. |
|
89 |
|
90 Let us, now move on to reading files line-by-line. |
|
91 |
|
92 %%1%% Pause the video here and re-open the file ``pendulum.txt`` |
|
93 with ``f`` as the file object, and then resume the video. |
|
94 |
|
95 We just use the up arrow until we reach the open command and issue |
|
96 it again. |
|
97 :: |
|
98 |
|
99 f = open('/home/fossee/pendulum.txt') |
|
100 |
|
101 Now, to read the file line-by-line, we iterate over the file |
|
102 object line-by-line, using the ``for`` command. Let us iterate over |
|
103 the file line-wise and print each of the lines. |
|
104 :: |
|
105 |
|
106 for line in f: |
|
107 print line |
|
108 |
|
109 As we already know, ``line`` is just a dummy variable, and not a |
|
110 keyword. We could have used any other variable name, but ``line`` |
|
111 seems meaningful enough. |
|
112 |
|
113 Instead of just printing the lines, let us append them to a list, |
|
114 ``line_list``. We first initialize an empty list, ``line_list``. |
|
115 :: |
|
116 |
|
117 line_list = [ ] |
|
118 |
|
119 Let us then read the file line-by-line and then append each of the |
|
120 lines, to the list. We could, as usual close the file using |
|
121 ``f.close`` and re-open it. But, this time, let's leave alone the |
|
122 file object ``f`` and directly open the file within the for |
|
123 statement. This will save us the trouble of closing the file, each |
|
124 time we open it. |
|
125 |
|
126 for line in open('/home/fossee/pendulum.txt'): |
|
127 line_list.append(line) |
|
128 |
|
129 Let us see what ``line_list`` contains. |
|
130 :: |
|
131 |
|
132 line_list |
|
133 |
|
134 Notice that ``line_list`` is a list of the lines in the file, along |
|
135 with the newline characters. If you noticed, ``pend_list`` did not |
|
136 contain the newline characters, because the string ``pend`` was |
|
137 split on the newline characters. |
|
138 |
|
139 {{{ show the summary slide }}} |
|
140 |
|
141 That brings us to the end of this tutorial. In this tutorial we |
|
142 have learnt to open and close files, read the data in the files as |
|
143 a whole, using the read command or reading it line by line by |
|
144 iterating over the file object. |
|
145 |
|
146 Thank you! |
|
147 |