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