getting-started-files/script.rst
author Anoop Jacob Thomas<anoop@fossee.in>
Tue, 12 Oct 2010 16:26:36 +0530
changeset 308 0a0a91fb3a0d
parent 245 3ed6ef2ea91f
child 345 d82151cc11f5
permissions -rw-r--r--
added slides for other-type-of-plots.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    18
   Internal Reviewer   : 
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    19
   External Reviewer   :
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    20
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    22
Script
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    23
------
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
{{{ Show the slide containing title }}}
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    26
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    27
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
    28
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
{{{ Show the outline for this tutorial }}} 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
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
    32
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
    33
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
    34
lines of a file to a list. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
{{{ switch back to the terminal }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
As usual, we start IPython, using 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
::
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
  ipython -pylab 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
Let us first open the file, ``pendulum.txt`` present in
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
``/home/fossee/``. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
::
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
  f = open('/home/fossee/pendulum.txt')
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
``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
    50
see what it is. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
::
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
  f
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
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
    56
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
    57
mode, here. 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
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
    60
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
    61
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
    62
into the variable ``pend``. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
::
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
  pend = f.read()
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
Now, let us see what is in ``pend``, by typing 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
::
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
  print pend
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
We can see that ``pend`` has all the data of file. Type just ``pend``
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
to see more explicitly, what it contains. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
::
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
  pend
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 242
diff changeset
    78
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
    79
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    80
%%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
    81
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
    82
variable has.
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    84
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
    85
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    86
.. #[punch: should this even be put? add dependency to strings LO,
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    87
.. where we mention that strings have methods for manipulation. hint:
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    88
.. use splitlines()]
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    89
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
::
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
  pend_list = pend.splitlines()
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
  pend_list
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    96
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
    97
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
    98
the end.
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    99
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   100
.. #[punch: should we mention file-pointer?]
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
Let us close the file opened into f.
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
  f.close()
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
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
   108
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
  f
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
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
   113
programming practice to close any file objects that we have
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
opened, after their job is done.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
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
   117
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   118
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
   119
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   120
%%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
   121
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   122
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
   123
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
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
   125
it again. 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
  f = open('/home/fossee/pendulum.txt')
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   130
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
   131
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
   132
the file line-wise and print each of the lines. 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
  for line in f:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
      print line
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
As we already know, ``line`` is just a dummy variable, and not a
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
keyword. We could have used any other variable name, but ``line``
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
seems meaningful enough.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
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
   143
``line_list``. We first initialize an empty list, ``line_list``. 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
  line_list = [ ]
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
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
   149
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
   150
``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
   151
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
   152
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
   153
time we open it. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
245
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
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 242
diff changeset
   157
  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
   158
      line_list.append(line)
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   159
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
Let us see what ``line_list`` contains. 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
  line_list
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
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
   166
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
   167
contain the newline characters, because the string ``pend`` was
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
split on the newline characters. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
{{{ show the summary slide }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
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
   173
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
   174
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
   175
iterating over the file object. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   177
{{{ 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
   178
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   179
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
   180
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   181
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
   182
Thank you!
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