Python file objects provide us with a wide range of methods and attributes that we can use to evaluate a file. These features include tools to determine the name of the file connected to the file object, whether it is open or closed, writable or readable, and how it handles errors.

In earlier tutorials, we already covered the file, its access methods, and how to open, close, read, and write files. It’s time to focus on some of the most significant and practical file management functions.

The output of a program can be displayed in a variety of ways when working with Python files. It can be presented in binary or human-readable form, or we can use the read() function to read some data.

What if we want to know where the file(read/write) pointer is located?

We make use of the tell() method to achieve this. An integer containing the file pointer’s current position in the file, represented as a number of bytes, is returned by the function f.tell(). Similar to a cursor, a file pointer or handler indicates where data must be read from or written to in the file. Knowing where the File Pointer is at a given time can be crucial. This assignment is simple to complete with the aid of tell().

Syntax: seek()

Parameters There are no settings needed.

Return Value: The file pointer’s current location within the file is returned by the seek() function.

f = open(“myfile.txt”, “r”)

print(f.readline() )


Here, the issue of what to do if we want to move the file pointer comes up.

Here, the seek() function is introduced.

When we open a file, the system points to the file’s beginning using the seek() function. Any read or write will begin immediately. Use the seek(offset, whence) function to modify the file object’s position. The whence parameter chooses the reference point, and the position is computed by adding the offset to the reference point. When working with an open file, it is helpful. Open the file, use the function seek(5) to move to where you want to start reading, and then go on reading the file if you want to read it but skip the first 5 bytes.

Syntax:  file_pointer .seek(offset, whence).

Offset: Offset is necessary for the seek() method. The read/write pointer’s position within the file is indicated by its offset.

Whence: This is not required. It establishes the reference point. When the default value is 0, absolute file placement is shown.

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.