Page 136 - PYTHON-12
P. 136

4.12 RANDOM ACCESS IN FILES USING TELL() AND SEEK()


              Till now in all our programs we laid stress on the sequential processing of data in a text and binary
              file. But files in Python allow random access of the data as well using built-in methods seek() and
              tell().
              seek()—seek() function is used to change the position of the file handle (file pointer) to a given
              specific position. File pointer is like a cursor, which defines from where the data has to be read
              or written in the file.

              Python file method seek() sets the file’s current position at the offset. This argument is optional
              and defaults to 0, which means absolute file positioning. Other values are: 1, which signifies seek is
              relative (may change) to the current position and 2 means seek is relative to the end of file. There
              is no return value.
              The reference point is defined by the "from_what" argument. It can have any of the three values:

              0: sets the reference point at the beginning of the file, which is by default.
              1: sets the reference point at the current file position.

              2: sets the reference point at the end of the file.

              seek() can be done in two ways:
              •  Absolute Positioning
              •  Relative Positioning

              Absolute referencing using seek() gives the file number on which the file pointer has to position
              itself. The syntax for seek() is—
                  f.seek(file_location)              #where f is the file pointer

              For example, f.seek(20) will give the position or file number where the file pointer has been placed.
              This statement shall move the file pointer to 20th byte in the file no matter where you are.
              Relative referencing/positioning has two arguments, offset and the position from which it has to
              traverse. The syntax for relative referencing is:

                  f.seek(offset, from_what)  #where f is file pointer

              For example,
              f.seek(–10,1) from current position, move 10 bytes backward

              f.seek(10,1) from current position, move 10 bytes forward
              f.seek(–20,1) from current position, move 20 bytes backward

              f.seek(10,0) from beginning of file, move 10 bytes forward
               POINT TO REMEMBER
               This is to be kept in mind that Python 3.x only supports text file seeks from the beginning of the file. seek()
               with negative offset only works when the file is opened in binary mode.

                                                                                                                  Python Libraries
              tell()—tell() returns the current position of the file read/write pointer within the file. Its syntax is:
                  f.tell()      #where f is file pointer




                                                                                                             4.7
   131   132   133   134   135   136   137   138   139   140   141