Table of Contents
show
seek
Function is used to change the position of the file handle to a given specific position. File handle is like a cursor, which defines from where the data has to be read or written in the file
Syntax
seek( )
Fileobj.seek(offset, pos)
- where Fileobj is the file pointer
- offset: Number of positions to move forward
- pos: it defines point of reference
- The reference point is selected by pos argument. It accepts three values:
- 0 : sets the reference point at the beginning of the file
- 1: sets the reference point at the current file position
- 2: sets the reference point at the end of the file
- By default, pos argument is set to 0
tell
Method can be used to get the position of the file handle. tell( ) method returns the current position of the file object. This method takes no parameters and returns an integer value. Initially file pointer points to the beginning of the file (If not opened in append mode). So, the initial value of tell( ) is zero
Syntax
tell( )
Fileobj.tell( )
Example
f = open("myfile4.txt","r")
f.seek(20)
print(f.tell())
print(f.readline())
f.close()
Input
To begin to toboggan first buy a toboggan, but don’t buy too big a toboggan.
Too big a toboggan is too big a toboggan to buy to begin to toboggan.
Output
20
first buy a toboggan, but don't buy too big a toboggan.
Views: 0