Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Files, Modules and Packag...
  5. Files
  6. File Position

File Position

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

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments