author | anand |
Thu, 11 Nov 2010 00:03:57 +0530 | |
changeset 472 | fcdec2d28c9a |
parent 389 | edf90e5d2a79 |
permissions | -rw-r--r-- |
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
1 |
.. Objectives |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
2 |
.. ---------- |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
3 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
4 |
.. By the end of this tutorial, you will be able to |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
5 |
.. 1. Open and read the contents of a file. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
6 |
.. #. Read files line by line. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
7 |
.. #. Read all the contents of the file at once. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
8 |
.. #. Close open files. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
9 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
10 |
.. Prerequisites |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
11 |
.. ------------- |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
12 |
|
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
13 |
.. 1. getting started with ipython |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
14 |
.. #. getting started with lists |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
15 |
.. #. getting started with for |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
16 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
17 |
.. Author : Puneeth |
345
d82151cc11f5
Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
245
diff
changeset
|
18 |
Internal Reviewer : Anoop Jacob Thomas<anoop@fossee.in> |
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
19 |
External Reviewer : |
388 | 20 |
Language Reviewer : Bhanukiran |
389 | 21 |
Checklist OK? : <06-11-2010, Anand, OK> [2010-10-05] |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
22 |
|
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
23 |
Script |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
24 |
------ |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
25 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
26 |
{{{ Show the slide containing title }}} |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
27 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
28 |
Hello Friends. Welcome to the tutorial on getting started with files. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
29 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
30 |
{{{ Show the outline for this tutorial }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
31 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
32 |
In this tutorial we shall learn to read files, and do some basic |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
33 |
actions on the file, like opening and reading a file, closing a |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
34 |
file, iterating through the file line-by-line, and appending the |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
35 |
lines of a file to a list. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
36 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
37 |
{{{ switch back to the terminal }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
38 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
39 |
As usual, we start IPython, using |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
40 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
41 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
42 |
ipython -pylab |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
43 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
44 |
Let us first open the file, ``pendulum.txt`` present in |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
45 |
``/home/fossee/``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
46 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
47 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
48 |
f = open('/home/fossee/pendulum.txt') |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
49 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
50 |
``f`` is called a file object. Let us type ``f`` on the terminal to |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
51 |
see what it is. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
52 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
53 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
54 |
f |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
55 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
56 |
The file object shows, the file which is open and the mode (read |
245
3ed6ef2ea91f
Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
242
diff
changeset
|
57 |
or write) in which it is open. Notice that it is open in read only |
3ed6ef2ea91f
Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
242
diff
changeset
|
58 |
mode, here. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
59 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
60 |
We shall first learn to read the whole file into a single |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
61 |
variable. Later, we shall look at reading it line-by-line. We use |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
62 |
the ``read`` method of ``f`` to read, all the contents of the file |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
63 |
into the variable ``pend``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
64 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
65 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
66 |
pend = f.read() |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
67 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
68 |
Now, let us see what is in ``pend``, by typing |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
69 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
70 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
71 |
print pend |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
72 |
|
387 | 73 |
We can see that ``pend`` has all the data of the file. Type just ``pend`` |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
74 |
to see more explicitly, what it contains. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
75 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
76 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
77 |
pend |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
78 |
|
245
3ed6ef2ea91f
Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
242
diff
changeset
|
79 |
Following is an exercise that you must do. |
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
80 |
|
379
4b3c0d8fffe2
Changes to getting started with files based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents:
345
diff
changeset
|
81 |
{{ show slide with Question 1 }} |
345
d82151cc11f5
Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
245
diff
changeset
|
82 |
|
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
83 |
%%1%% Split the variable into a list, ``pend_list``, of the lines in |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
84 |
the file. Hint, use the tab command to see what methods the string |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
85 |
variable has. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
86 |
|
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
87 |
Please, pause the video here. Do the exercise and then continue. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
88 |
|
379
4b3c0d8fffe2
Changes to getting started with files based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents:
345
diff
changeset
|
89 |
{{ show slide with Solution 1 }} |
345
d82151cc11f5
Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
245
diff
changeset
|
90 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
91 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
92 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
93 |
pend_list = pend.splitlines() |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
94 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
95 |
pend_list |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
96 |
|
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
97 |
Now, let us learn to read the file line-by-line. But, before that we |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
98 |
will have to close the file, since the file has already been read till |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
99 |
the end. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
100 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
101 |
Let us close the file opened into f. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
102 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
103 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
104 |
f.close() |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
105 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
106 |
Let us again type ``f`` on the prompt to see what it shows. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
107 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
108 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
109 |
f |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
110 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
111 |
Notice, that it now says the file has been closed. It is a good |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
112 |
programming practice to close any file objects that we have |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
113 |
opened, after their job is done. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
114 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
115 |
Let us, now move on to reading files line-by-line. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
116 |
|
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
117 |
Following is an exercise that you must do. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
118 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
119 |
%%2%% Re-open the file ``pendulum.txt`` with ``f`` as the file object. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
120 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
121 |
Please, pause the video here. Do the exercise and then continue. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
122 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
123 |
We just use the up arrow until we reach the open command and issue |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
124 |
it again. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
125 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
126 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
127 |
f = open('/home/fossee/pendulum.txt') |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
128 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
129 |
Now, to read the file line-by-line, we iterate over the file |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
130 |
object line-by-line, using the ``for`` command. Let us iterate over |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
131 |
the file line-wise and print each of the lines. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
132 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
133 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
134 |
for line in f: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
135 |
print line |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
136 |
|
379
4b3c0d8fffe2
Changes to getting started with files based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents:
345
diff
changeset
|
137 |
As we already know, ``line`` is variable, sometimes called the loop |
4b3c0d8fffe2
Changes to getting started with files based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents:
345
diff
changeset
|
138 |
variable, and it is not a keyword. We could have used any other |
4b3c0d8fffe2
Changes to getting started with files based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents:
345
diff
changeset
|
139 |
variable name, but ``line`` seems meaningful enough. |
345
d82151cc11f5
Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
245
diff
changeset
|
140 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
141 |
Instead of just printing the lines, let us append them to a list, |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
142 |
``line_list``. We first initialize an empty list, ``line_list``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
143 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
144 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
145 |
line_list = [ ] |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
146 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
147 |
Let us then read the file line-by-line and then append each of the |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
148 |
lines, to the list. We could, as usual close the file using |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
149 |
``f.close`` and re-open it. But, this time, let's leave alone the |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
150 |
file object ``f`` and directly open the file within the for |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
151 |
statement. This will save us the trouble of closing the file, each |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
152 |
time we open it. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
153 |
|
245
3ed6ef2ea91f
Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
242
diff
changeset
|
154 |
:: |
3ed6ef2ea91f
Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
242
diff
changeset
|
155 |
|
3ed6ef2ea91f
Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
242
diff
changeset
|
156 |
for line in open('/home/fossee/pendulum.txt'): |
3ed6ef2ea91f
Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
242
diff
changeset
|
157 |
line_list.append(line) |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
158 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
159 |
Let us see what ``line_list`` contains. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
160 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
161 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
162 |
line_list |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
163 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
164 |
Notice that ``line_list`` is a list of the lines in the file, along |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
165 |
with the newline characters. If you noticed, ``pend_list`` did not |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
166 |
contain the newline characters, because the string ``pend`` was |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
167 |
split on the newline characters. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
168 |
|
379
4b3c0d8fffe2
Changes to getting started with files based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents:
345
diff
changeset
|
169 |
Using some string methods, that we shall look at in the tutorial on |
4b3c0d8fffe2
Changes to getting started with files based on review.
Puneeth Chaganti <punchagan@fossee.in>
parents:
345
diff
changeset
|
170 |
strings, we can strip out the newline characters from the lines. |
345
d82151cc11f5
Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
245
diff
changeset
|
171 |
|
d82151cc11f5
Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
245
diff
changeset
|
172 |
.. #[[Anoop: I think the code that are required to be typed can be |
d82151cc11f5
Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
245
diff
changeset
|
173 |
added to the slide.]] |
d82151cc11f5
Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
245
diff
changeset
|
174 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
175 |
{{{ show the summary slide }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
176 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
177 |
That brings us to the end of this tutorial. In this tutorial we |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
178 |
have learnt to open and close files, read the data in the files as |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
179 |
a whole, using the read command or reading it line by line by |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
180 |
iterating over the file object. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
181 |
|
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
182 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
183 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
184 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
185 |
|
242
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
186 |
Hope you have enjoyed and found it useful. |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
187 |
Thank you! |
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
188 |
|
a33e942379d7
Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
189 |