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 |
52 to see more explicitly, what it contains. |
72 to see more explicitly, what it contains. |
53 :: |
73 :: |
54 |
74 |
55 pend |
75 pend |
56 |
76 |
57 %%1%% Pause the video here and split the variable into a list, |
77 Following is an (are) exercise(s) 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 |
78 |
62 #[punch: should this even be put? add dependency to strings LO, |
79 %%1%% Split the variable into a list, ``pend_list``, of the lines in |
63 where we mention that strings have methods for manipulation. hint: |
80 the file. Hint, use the tab command to see what methods the string |
64 use splitlines()] |
81 variable has. |
|
82 |
|
83 Please, pause the video here. Do the exercise and then continue. |
|
84 |
|
85 .. #[punch: should this even be put? add dependency to strings LO, |
|
86 .. where we mention that strings have methods for manipulation. hint: |
|
87 .. use splitlines()] |
|
88 |
65 :: |
89 :: |
66 |
90 |
67 pend_list = pend.splitlines() |
91 pend_list = pend.splitlines() |
68 |
92 |
69 pend_list |
93 pend_list |
70 |
94 |
71 Now, let us learn to read the file line-by-line. But, before that |
95 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 |
96 will have to close the file, since the file has already been read till |
73 read till the end. |
97 the end. |
74 #[punch: should we mention file-pointer?] |
98 |
|
99 .. #[punch: should we mention file-pointer?] |
75 |
100 |
76 Let us close the file opened into f. |
101 Let us close the file opened into f. |
77 :: |
102 :: |
78 |
103 |
79 f.close() |
104 f.close() |
87 programming practice to close any file objects that we have |
112 programming practice to close any file objects that we have |
88 opened, after their job is done. |
113 opened, after their job is done. |
89 |
114 |
90 Let us, now move on to reading files line-by-line. |
115 Let us, now move on to reading files line-by-line. |
91 |
116 |
92 %%1%% Pause the video here and re-open the file ``pendulum.txt`` |
117 Following is an exercise that you must do. |
93 with ``f`` as the file object, and then resume the video. |
118 |
|
119 %%2%% Re-open the file ``pendulum.txt`` with ``f`` as the file object. |
|
120 |
|
121 Please, pause the video here. Do the exercise and then continue. |
94 |
122 |
95 We just use the up arrow until we reach the open command and issue |
123 We just use the up arrow until we reach the open command and issue |
96 it again. |
124 it again. |
97 :: |
125 :: |
98 |
126 |
141 That brings us to the end of this tutorial. In this tutorial we |
169 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 |
170 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 |
171 a whole, using the read command or reading it line by line by |
144 iterating over the file object. |
172 iterating over the file object. |
145 |
173 |
146 Thank you! |
174 {{{ Show the "sponsored by FOSSEE" slide }}} |
147 |
175 |
|
176 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
177 |
|
178 Hope you have enjoyed and found it useful. |
|
179 Thank you! |
|
180 |
|
181 |