getting-started-files/script.rst
changeset 245 3ed6ef2ea91f
parent 242 a33e942379d7
child 345 d82151cc11f5
equal deleted inserted replaced
244:b67d5e07cab5 245:3ed6ef2ea91f
    51 ::
    51 ::
    52 
    52 
    53   f
    53   f
    54 
    54 
    55 The file object shows, the file which is open and the mode (read
    55 The file object shows, the file which is open and the mode (read
    56 or write) in which it is open. 
    56 or write) in which it is open. Notice that it is open in read only
       
    57 mode, here. 
    57 
    58 
    58 We shall first learn to read the whole file into a single
    59 We shall first learn to read the whole file into a single
    59 variable. Later, we shall look at reading it line-by-line. We use
    60 variable. Later, we shall look at reading it line-by-line. We use
    60 the ``read`` method of ``f`` to read, all the contents of the file
    61 the ``read`` method of ``f`` to read, all the contents of the file
    61 into the variable ``pend``. 
    62 into the variable ``pend``. 
    72 to see more explicitly, what it contains. 
    73 to see more explicitly, what it contains. 
    73 ::
    74 ::
    74 
    75 
    75   pend
    76   pend
    76 
    77 
    77 Following is an (are) exercise(s) that you must do. 
    78 Following is an exercise that you must do. 
    78 
    79 
    79 %%1%% Split the variable into a list, ``pend_list``, of the lines in
    80 %%1%% Split the variable into a list, ``pend_list``, of the lines in
    80 the file. Hint, use the tab command to see what methods the string
    81 the file. Hint, use the tab command to see what methods the string
    81 variable has.
    82 variable has.
    82 
    83 
   149 ``f.close`` and re-open it. But, this time, let's leave alone the
   150 ``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 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 statement. This will save us the trouble of closing the file, each
   152 time we open it. 
   153 time we open it. 
   153 
   154 
   154 for line in open('/home/fossee/pendulum.txt'):
   155 ::
   155 line_list.append(line)
   156 
       
   157   for line in open('/home/fossee/pendulum.txt'):
       
   158       line_list.append(line)
   156 
   159 
   157 Let us see what ``line_list`` contains. 
   160 Let us see what ``line_list`` contains. 
   158 ::
   161 ::
   159 
   162 
   160   line_list
   163   line_list