Cursor position with tell()

EasyFile Handling Programs · 31 of 38

Write a Python program to find the current cursor position in a file.

Advertisement
Output
0
5

Approach

  1. tell() returns the position in bytes from the start.

Solution

Python
with open("story.txt", "w") as f:    f.write("Hello Python\n") with open("story.txt") as f:    print(f.tell())    f.read(5)    print(f.tell())
Output
0
5

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs

Leave a Comment