Python Practice › File Handling Programs › Program 32
Move the cursor with seek()
Write a Python program to move the cursor to a specific position in a file.
Output
Python Hello
Approach
seek(n)moves to byte n; reading continues from there.
Solution
with open("story.txt", "w") as f: f.write("Hello Python\n") with open("story.txt") as f: f.seek(6) print(f.read().strip()) f.seek(0) print(f.read(5))Output
Python Hello
Learn the concept
New to this topic? Read the lesson, then come back.