getting-started-files/script.rst
author amit
Wed, 27 Oct 2010 15:13:17 +0530
changeset 362 a77a27916f81
parent 345 d82151cc11f5
child 379 4b3c0d8fffe2
permissions -rw-r--r--
Added Objectives and other metadata
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
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   :
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
345
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
    80
.. #[[Anoop:add context switch to next slide - questions]]
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
    81
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    82
%%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
    83
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
    84
variable has.
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    86
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
    87
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    88
.. #[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
    89
.. 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
    90
.. use splitlines()]
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    91
345
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
    92
.. #[[Anoop: let us have it here, let us consider this as a
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
    93
   refresher]]
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
    94
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
    95
.. #[[Anoop:add context switch to next slide - solution]]
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
    96
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
  pend_list = pend.splitlines()
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
  pend_list
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   103
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
   104
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
   105
the end.
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   106
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   107
.. #[punch: should we mention file-pointer?]
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   108
345
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   109
.. #[[Anoop: I think we can say that ``f`` is a file pointer which
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   110
   points to the next line/data to be read from the file. We could
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   111
   skip details.]]
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   112
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
Let us close the file opened into f.
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
  f.close()
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
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
   119
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
  f
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
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
   124
programming practice to close any file objects that we have
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
opened, after their job is done.
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
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
   128
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   129
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
   130
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   131
%%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
   132
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   133
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
   134
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
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
   136
it again. 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
  f = open('/home/fossee/pendulum.txt')
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
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
   142
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
   143
the file line-wise and print each of the lines. 
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
  for line in f:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
      print line
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
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
   150
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
   151
seems meaningful enough.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
345
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   153
.. #[[Anoop: using dummy variable doesn't seem correct, can say line
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   154
   is a variable]]
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   155
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
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
   157
``line_list``. We first initialize an empty list, ``line_list``. 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
  line_list = [ ]
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
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
   163
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
   164
``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
   165
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
   166
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
   167
time we open it. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 242
diff changeset
   169
::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 242
diff changeset
   170
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 242
diff changeset
   171
  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
   172
      line_list.append(line)
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
Let us see what ``line_list`` contains. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
::
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
  line_list
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
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
   180
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
   181
contain the newline characters, because the string ``pend`` was
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
split on the newline characters. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
345
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   184
.. #[[Anoop: I think we need to tell them that each line can be
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   185
   stripped and appended to list to avoid the problem of newline
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   186
   characters.]]
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   187
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   188
.. #[[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
   189
   added to the slide.]]
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   190
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   191
.. #[[Anoop: Context switches are to be added.]]
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 245
diff changeset
   192
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
{{{ show the summary slide }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   194
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   195
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
   196
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
   197
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
   198
iterating over the file object. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   199
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   200
{{{ 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
   201
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   202
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
   203
242
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   204
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
   205
Thank you!
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   206
a33e942379d7 Cleaned up script for getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   207