1 ======== |
1 .. Objectives |
2 Script |
2 .. ---------- |
3 ======== |
|
4 |
3 |
5 Welcome to the tutorial on getting started with files. |
4 .. By the end of this tutorial, you will be able to |
|
5 .. 1. Open and read the contents of a file. |
|
6 .. #. Read files line by line. |
|
7 .. #. Read all the contents of the file at once. |
|
8 .. #. Close open files. |
6 |
9 |
7 {{{ Screen shows welcome slide }}} |
10 .. Prerequisites |
|
11 .. ------------- |
|
12 |
|
13 .. 1. getting started with ipython |
|
14 .. #. getting started with lists |
|
15 .. #. getting started with for |
|
16 |
|
17 .. Author : Puneeth |
|
18 Internal Reviewer : |
|
19 External Reviewer : |
|
20 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
21 |
|
22 Script |
|
23 ------ |
|
24 |
|
25 {{{ Show the slide containing title }}} |
|
26 |
|
27 Hello Friends. Welcome to the tutorial on getting started with files. |
8 |
28 |
9 {{{ Show the outline for this tutorial }}} |
29 {{{ Show the outline for this tutorial }}} |
10 |
30 |
11 In this tutorial we shall learn to read files, and do some basic |
31 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 |
32 actions on the file, like opening and reading a file, closing a |
31 :: |
51 :: |
32 |
52 |
33 f |
53 f |
34 |
54 |
35 The file object shows, the file which is open and the mode (read |
55 The file object shows, the file which is open and the mode (read |
36 or write) in which it is open. |
56 or write) in which it is open. Notice that it is open in read only |
|
57 mode, here. |
37 |
58 |
38 We shall first learn to read the whole file into a single |
59 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 |
60 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 |
61 the ``read`` method of ``f`` to read, all the contents of the file |
41 into the variable ``pend``. |
62 into the variable ``pend``. |
52 to see more explicitly, what it contains. |
73 to see more explicitly, what it contains. |
53 :: |
74 :: |
54 |
75 |
55 pend |
76 pend |
56 |
77 |
57 %%1%% Pause the video here and split the variable into a list, |
78 Following is an exercise that you must do. |
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 |
79 |
62 #[punch: should this even be put? add dependency to strings LO, |
80 %%1%% Split the variable into a list, ``pend_list``, of the lines in |
63 where we mention that strings have methods for manipulation. hint: |
81 the file. Hint, use the tab command to see what methods the string |
64 use splitlines()] |
82 variable has. |
|
83 |
|
84 Please, pause the video here. Do the exercise and then continue. |
|
85 |
|
86 .. #[punch: should this even be put? add dependency to strings LO, |
|
87 .. where we mention that strings have methods for manipulation. hint: |
|
88 .. use splitlines()] |
|
89 |
65 :: |
90 :: |
66 |
91 |
67 pend_list = pend.splitlines() |
92 pend_list = pend.splitlines() |
68 |
93 |
69 pend_list |
94 pend_list |
70 |
95 |
71 Now, let us learn to read the file line-by-line. But, before that |
96 Now, let us learn to read the file line-by-line. But, before that we |
72 we will have to close the file, since the file has already been |
97 will have to close the file, since the file has already been read till |
73 read till the end. |
98 the end. |
74 #[punch: should we mention file-pointer?] |
99 |
|
100 .. #[punch: should we mention file-pointer?] |
75 |
101 |
76 Let us close the file opened into f. |
102 Let us close the file opened into f. |
77 :: |
103 :: |
78 |
104 |
79 f.close() |
105 f.close() |
87 programming practice to close any file objects that we have |
113 programming practice to close any file objects that we have |
88 opened, after their job is done. |
114 opened, after their job is done. |
89 |
115 |
90 Let us, now move on to reading files line-by-line. |
116 Let us, now move on to reading files line-by-line. |
91 |
117 |
92 %%1%% Pause the video here and re-open the file ``pendulum.txt`` |
118 Following is an exercise that you must do. |
93 with ``f`` as the file object, and then resume the video. |
119 |
|
120 %%2%% Re-open the file ``pendulum.txt`` with ``f`` as the file object. |
|
121 |
|
122 Please, pause the video here. Do the exercise and then continue. |
94 |
123 |
95 We just use the up arrow until we reach the open command and issue |
124 We just use the up arrow until we reach the open command and issue |
96 it again. |
125 it again. |
97 :: |
126 :: |
98 |
127 |
121 ``f.close`` and re-open it. But, this time, let's leave alone the |
150 ``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 |
151 file object ``f`` and directly open the file within the for |
123 statement. This will save us the trouble of closing the file, each |
152 statement. This will save us the trouble of closing the file, each |
124 time we open it. |
153 time we open it. |
125 |
154 |
126 for line in open('/home/fossee/pendulum.txt'): |
155 :: |
127 line_list.append(line) |
156 |
|
157 for line in open('/home/fossee/pendulum.txt'): |
|
158 line_list.append(line) |
128 |
159 |
129 Let us see what ``line_list`` contains. |
160 Let us see what ``line_list`` contains. |
130 :: |
161 :: |
131 |
162 |
132 line_list |
163 line_list |
141 That brings us to the end of this tutorial. In this tutorial we |
172 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 |
173 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 |
174 a whole, using the read command or reading it line by line by |
144 iterating over the file object. |
175 iterating over the file object. |
145 |
176 |
146 Thank you! |
177 {{{ Show the "sponsored by FOSSEE" slide }}} |
147 |
178 |
|
179 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
180 |
|
181 Hope you have enjoyed and found it useful. |
|
182 Thank you! |
|
183 |
|
184 |