Saturday 5 December 2015

Python: seek: Change file object position


‘seek(n)’ is used to go to the nth byte in the file.

seek(offset, from_what)
The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.
>>> f = open('/Users/harikrishna_gurram/abc.txt', 'rb+')
>>> f.write(b'0123456789abcdef')
16
>>> f.read(1)
b''
>>> f.seek(8)
8
>>> f.read(1)
b'8'



Previous                                                 Next                                                 Home

No comments:

Post a Comment