Skip to content

How to change file pointer position in Python

How to change file pointer position in Python

In this tutorial we will learn about how to change file pointer position in python . Before reading further we recommend to read about these tutorials –

Here we will use the file “hello1.txt” and we will keep the file in the current folder where we are working now .

Here is the content of hello1.txt –

hello World1
hello World2
hello World3hello world 1
hello World2
hello World3

Now before changing position of the file pointer first we should know about what is the current position of the file pointer . To know the current position of the file pointer Python File provides a built-in tell() method .

Python File tell() method | How to know the current position of the file pointer in Python ?

The tell() method allows us to know the current location of the file-pointer. The tell method returns an integer. Since the file-pointer is at the very beginning of the file when you open the file in “r” and “w” mode, tell method will return 0 . But if we open the file in “a” mode, since the file-pointer is at the end of the file, the tell method will return the last position.

Syntax of tell method

file_object.tell()

Return type of tell method

It returns an integer specifying the position of the file-pointer .

Examples of Python File tell() method

Example 1: Check the current position of the file pointer after opening a file

# file opened with "r" mode
with open("hello1.txt","r") as fp:
    print("position for r mode =",fp.tell())
    
# file opened with "w" mode    
with open("hello1.txt","w") as fp:
    print("position for w mode =",fp.tell())
    
# file opened with "a" mode    
with open("hello1.txt","a") as fp:
    print("position for a mode =",fp.tell())
position for r mode = 0
position for w mode = 0
position for a mode = 0
  • In the above code for the first time , the file has been opened in “r” mode, so tell() method has returned 0 .
  • Second time we have opened the file in “w” mode , so tell() method has returned 0 .
  • But to open the file in “w” mode, the entire contents of the file are deleted, so when the file is opened in “a” mode, the tell() method returns 0.

Example 2: Check the current position of the file pointer after writing to a file

# file opened with "r" mode
with open("hello1.txt","r") as fp:
    print("for r mode position =",end=" ")
    print(fp.tell())
    
# file opened with "w" mode    
with open("hello1.txt","w") as fp:
    print("for w mode position",end=" ")
    print("before =",fp.tell())
    fp.write("Welcome to python")
    print("after =",fp.tell())
    
# file opened with "a" mode    
with open("hello1.txt","a") as fp:
    print("for a mode position",end=" ")
    print("before =",fp.tell())
    fp.write("python is a easy programming language")
    print("after =",fp.tell())
for r mode position = 0
for w mode position before = 0
after = 17
for a mode position before = 17
after = 54

For the first time in the above code, the file has been opened in “r” mode, so tell() method has returned 0 .

Second time we have opened the file in “w” mode , so tell() method has returned 0 . But after writing something in “w” mode, when the file-pointer is moved, the tell() method returns the position 17 of that moved file-pointer.

Then when the file is opened in “a” mode, the tell() method returns the last position of the file that is 17 . Then, after writing something in “a” mode, when the file-pointer is moved, the tell() method returns the position 54 of the moved file-pointer .

So we have learned about file pointer and how to know the current position of the file pointer . Now we will learn about how to change file pointer position in python . For this reason Python File Provides built-in seek() method .

seek method

Seek method helps us to change the current position of the file-pointer.

Syntax of seek method

file_object.seek(offset, whence)

Parameters of seek method

  • offset = It is an integer specifying the distance from the current position to the new position
  • whence = it is an integer ,defines point of reference . Default value is 0 which means from start . Other values are 1 which means from the current position and 2 which means from the end .

Note

In case of .txt file whence 1 or 2 works properly only when offset is 0 otherwise it doesn’t always work properly. Therefore, in the .txt file, we should always use whence as 0 . In case of txt file, offset should always be used using fp.tell () method, otherwise the program may behave anonymously. Everything works fine in the case of .bin file.

Examples of Python File seek() method

Example 1: Change the position of the file pointer after opening a file in reading mode

with open("hello1.txt","r") as fp:
    print(fp.read())
    print("After reading the file position of file pointer is =",fp.tell())
    
    #taking filepointer at the beginning with reference 0(start)
    fp.seek(0,0)
    print("After taking the file pointer at 0th positioon position of file pointer is =",fp.tell())
    
    #taking filepointer at the ending with reference 2(end)
    fp.seek(0,2)
    print("After taking the file pointer at end positioon position of file pointer is =",fp.tell())
    
    fp.seek(0,0)
    fp.read(7)
    #after reading 7 letters position of filepointer    
    print("after reading 7 letters position of filepointer is =",fp.tell()) 
    
    #if we want to take the filepointer after 14 letters from this position
    fp.seek(fp.tell()+14,0)
    #in the above line we can write as fp.seek(21,0) 
    #but it is not recommended as it may cause undefined behaviour
    #in the position of offset only use 0 as absolute number
    #otherwise use fp.tell() +/- offset
    print("after reading 14 letters more position of filepointer is =",fp.tell())
    
    print(fp.read())
    #after reading the file filepointer reached at the end
    #to take filepointer 8 letters ahead
    fp.seek(fp.tell()-8,0)
    print("After shifting the file pointer 8 letters ahead from end position of file pointer is =",fp.tell())
    print(fp.read())
Welcome to pythonpython is a easy programming language
After reading the file position of file pointer is = 54
After taking the file pointer at 0th positioon position of file pointer is = 0
After taking the file pointer at end positioon position of file pointer is = 54
after reading 7 letters position of filepointer is = 7
after reading 14 letters more position of filepointer is = 21
on is a easy programming language
After shifting the file pointer 8 letters ahead from end position of file pointer is = 46
language
  • In the above code first we have read the entire file . As a result of reading the entire file first , the file-pointer has reached the very end for which fp.tell() has returned 54.
  • Later, with the help of fp.seek(0,0) method, file-pointer was brought to the very beginning for which fp.tell() returned 0.
  • Then with the help of fp.seek(0,2) method the file-pointer is taken to the very end for which fp.tell() has returned 54.
  • Then again taking the file-pointer at the very beginning we have read 7 letters for which fp.tell() has returned 7.
  • We then used fp.tell() + 14 in offset to move the file-pointer 14 letters further away but we could use 21 (7+14) directly in offset but it could behave anonymously. It is always recommended that if the file-pointer is at beginning then we can use offset as 0 directly or otherwise we should use fp.tell() method in offset . As a result, fp.tell() has returned 21 .
  • Then we read the rest of the file after that fp.tell() has returned 54.
  • We used fp.tell() - 8 in offset to take file-pointer 8 letters before the last position, after which fp.tell() returned 8. Then the rest is read again.

Example 2: Change the position of the file pointer after opening a file in writing mode

with open("hello1.txt","w") as fp:
    print("After opening position of file pointer is =",fp.tell())
    
    fp.write("Python was invented in 1993 by Guido van Rossum")
    #filepointer reached at the end 
    print("After writting position of file pointer is =",fp.tell())
    
    #taking filepointer 24 letters back where 1 start
    fp.seek(fp.tell()-24,0)
    print("After taking filepointer 24 letters back, position of file pointer is =",fp.tell())
    
    #overwrite 1993 with 1991
    fp.write("1991")
After opening position of file pointer is = 0
After writting position of file pointer is = 47
After taking filepointer 24 letters back, position of file pointer is = 23
  • In the above code after opening the file, all the old content is deleted because the file has been opened in “w” mode for which fp.tell() has returned 0.
  • After writing something in the file, file-pointer has reached the end for which fp.tell() has returned 47.
  • Then the file-pointer was brought back 24 letters ahead from the end with the help of fp.seek(fp.tell()-24,0) method for which fp.tell() has returned 23.
  • Then we have written "1991" which overwrites "1993".
This image has an empty alt attribute; its file name is 8.png

Example 3: Change the position of the file pointer after opening a file in append mode

with open("hello1.txt","a") as fp:
    #filepointer at the end position that is 47
    print("After opening the position of file pointer is =",fp.tell())
    
    fp.seek(fp.tell()-24,0)
    #filepointer at 23rd position
    print("After taking the file pointer 24 letters ahead the position of file pointer is =",fp.tell())
    
    #wherever the filepointer appending always done at the end
    fp.write("1993")
    print("After writting 1993 the position of file pointer is =",fp.tell())
    
    fp.seek(fp.tell()-2,0)
    print(fp.tell())
    
    fp.write("Python")
    print("After writting Python the position of file pointer is =",fp.tell())
After opening the position of file pointer is = 47
After taking the file pointer 24 letters ahead the position of file pointer is = 23
After writting 1993 the position of file pointer is = 51
49
After writting Python the position of file pointer is = 57
  • In the above code in order to open the file in “a” mode , the file-pointer first reaches the end for which fp.tell() has returned 47.
  • Then file-pointer has been brought back to 24 letters for which fp.tell() has returned 23.
  • Then "1993" is written in the file but to open the file in “a” mode the text is added at the very end without overwriting the content of the 23rd position for which fp.tell() later returned 51(47+4) .
  • Then again file-pointer has been brought back to 2 letters which is 49th position . Then "Python" is written in the file . But here also the text has been added at the end of the previous writing. For which fp.tell() has returned 57 (51+6).
This image has an empty alt attribute; its file name is 9.png

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply