Seek and tell methods binary files in python

Seek() and Tell() methods to work with Binary files in Python

This post will discuss seek() and tell() methods to work with Binary files in Python. In binary files, the data is stored in the form of bytes. We know that. When we perform a write or read operation on a binary file, a file pointer moves based on how many bytes of data are read or written from the file.


Link to all chapters of our Python tutorial series: Learn Python


Suppose, if we read 10 bytes of data from a file, the position of the file pointer will be placed at the 10th byte so that it can continue to read from the 11th byte. We can use the tell() method to find the position of a pointer. Please see below its format.

#format of tell() method
num = f.tell()

In the format mentioned above, ‘f‘ is the file handler in your program and ‘num‘ is more like a variable that will have the value from f.tell() method.

#format of seek() method
f.seek(offset, fromwhere)

In the format mentioned above, ‘f‘ is the file handler. The ‘offset‘ refers to the number of bytes to move. The ‘fromwhere‘ refers to the position from which we can move it. For instance, the ‘fromwhere‘ can be 0, 1 or 2.

If the ‘fromwhere’ is set to 0, it refers to the beginning of the file; if it is ‘1’, it relates to the current position of the file, and if it is ‘2’, it refers to the ending of the file.

  • f.seek(10) is same as f.seek(10,0). It means that the file pointer will be placed at the 11th byte (10+1) from the beginning of the file (0).
  • f.seek(-10,2) means that the file pointer refers to the 9th byte (-10+1) from the ending of the file (2).

Let us look at an example to understand how we can use the seek() and tell() methods work.

#Python program to understand the concept of seek and tell methods

with open('samplefile.txt','r+b') as f:
  f.write(b'Warning Signal')
  f.seek(3) #points the file pointer to 4th byte
  print(f.read(2)) #prints two bytes from the pointer's location
  print(f.tell()) #displays the current location of the pointer
  f.seek(-6,2) #moves the pointer to 5th position (-6+1) from the end
  print(f.read(1)) #prints one byte from the current pointer's location
  print(f.tell()) #displays the current location of the pointer
Output:

b'ni'
5
b'S'
9

Before running the program above, we created an empty file with the name ‘samplefile.txt‘.

The program above opens the file in ‘r+b‘ mode so we can read and write data in the file. Using the write method, we added the text’ Warning Signal‘ in the file in binary form since we prefixed the text with ‘b‘.

The string gets stored in the file, as shown in the image below.

String Characters and the byte positions

Using f.seek(3), we move the file pointer to the fourth byte (3+1 = 4). So the file pointer is currently situated at the character ‘n‘. Now when we use f.read(2), it prints the 2 bytes from this position, which is ‘ni‘.

When we use f.tell() now, it gives us the current file pointer location as 5.

Using f.seek(-6,2) will set the file pointer to the 5th position (-6+1 = 5) from the end of the file. It is currently positioned at ‘S‘. That’s why it prints only’ S‘ when we print the current 1 byte using f.read(1).

Using the f.tell() method will let us know the current position of the file pointer.

We can randomly access data from any position using the seek() and tell() methods. Suppose there are 1000 bytes in a file, and if you want to access the last 10 bytes of the data, it is not advisable to go through the entire file byte by byte. Instead, you can position the file pointer at the required location using f.seek(990). Now, if you use f.read(10), it will read the last 10 bytes of your 1000 byte data file.

Now let us look at a program that can retrieve only the word Signal from the binary file we created.

#Python program to retrieve data from a binary file

with open('samplefile.txt','rb') as f:
  f.seek(8)
  print(f.read(6))
Output:

b'Signal'

We are opening the same binary file in binary read mode in the above program. We positioned the file pointer to the desired location using the f.seek(8). After this, when we printed the next 6 bytes, it printed Signal as required. The output is prefixed with ‘b’ since the data is of byte type.

Scroll to Top