Get the Current File Pointer Position Using tell() in Python

In Python, the tell() method is used to determine the current position of the file pointer while working with files. This method returns an integer representing the byte position of the pointer from the beginning of the file. It is particularly useful when you need to track file reading or writing positions.


Examples

1. Getting the Initial File Pointer Position

When a file is opened, the pointer starts at the beginning (position 0). Let’s check the initial position using tell().

In this example, we will open a file example.txt, and get the initial position of the file pointer, and print it to console output.

main.py

</>
Copy
# Opening a file in read mode
with open("example.txt", "r") as file:
    # Getting the initial file pointer position
    position = file.tell()
    print("Initial File Pointer Position:", position)

Explanation

Here, we:

  1. Open the file example.txt in read mode using open().
  2. Use file.tell() to get the current position of the pointer.
  3. Print the result, which should be 0 since the pointer is at the beginning.

Output:

2. Checking Pointer Position After Reading Characters

The file pointer moves forward when reading characters. We can use tell() after reading some content to check the new position.

main.py

</>
Copy
# Opening a file in read mode
with open("example.txt", "r") as file:
    # Reading first 10 characters
    content = file.read(10)
    
    # Getting the current file pointer position
    position = file.tell()
    
    print("After reading 10 characters, File Pointer Position:", position)

Explanation

In this example:

  1. We open example.txt in read mode.
  2. The read(10) method reads the first 10 characters of the file.
  3. The pointer moves forward by 10 bytes.
  4. We use tell() to check the current position.

Output:

3. Checking Pointer Position After Using seek()

The seek() method allows moving the file pointer to a specific position. We can use tell() to verify the position after using seek().

main.py

</>
Copy
# Opening a file in read mode
with open("example.txt", "r") as file:
    # Moving the file pointer to position 5
    file.seek(5)
    
    # Getting the current file pointer position
    position = file.tell()
    
    print("File Pointer Position after seek(5):", position)

Explanation

Here:

  1. We open example.txt in read mode.
  2. The seek(5) method moves the pointer to the 5th byte in the file.
  3. We use tell() to check the new position.

Output:

4. Pointer Position While Writing to a File

When writing data to a file, the pointer moves forward. We can use tell() to check the position after writing.

main.py

</>
Copy
# Opening a file in write mode
with open("example.txt", "w") as file:
    # Writing text to file
    file.write("Hello World!")
    
    # Getting the file pointer position
    position = file.tell()
    
    print("File Pointer Position after writing:", position)

Explanation

In this case:

  1. We open example.txt in write mode.
  2. The write() method writes “Hello World!” to the file.
  3. The pointer moves to the end of the written text (position 12).
  4. tell() confirms the position.

Output:

Conclusion

The tell() method helps track the file pointer position when working with files in Python. Key takeaways:

  1. At the beginning of a file, tell() returns 0.
  2. After reading data, the pointer moves forward.
  3. Using seek() moves the pointer to a specified position.
  4. Writing data moves the pointer to the end of the written content.