seek() function in Python Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 39 Likes Like Report In Python, the seek() function is used to move the file cursor to a specific position inside a file. This allows you to read or write at any part of the file instead of always starting from the beginning. For example, if you want to skip the first 10 characters while reading a file, you can do: Python f = open("demo.txt", "r") f.seek(10) print(f.read()) f.close() This will move the file cursor to position 10 and begin reading from there.Syntax: file.seek(offset, from_what)Parameters:offset: Number of bytes to move the cursor.from_what: (optional) Reference point to start from:0: sets the reference point at the beginning of the file 1: sets the reference point at the current file position 2: sets the reference point at the end of the file Returns:Returns the new absolute cursor position from the beginning.Note: In text mode, from_what can only be 0. Using 1 or 2 requires binary mode ('rb').ExamplesExample 1: Using seek() in Text ModeLet's say GfG.txt contains:Code is like humor. When you have to explain it, it’s bad. Python f = open("GfG.txt", "r") f.seek(20) print(f.tell()) print(f.readline()) f.close() Output:20When you have to explain it, it’s bad.Explanation:seek(20) moves the cursor to character 20.tell() confirms the cursor is at position 20.readline() reads from that position onward.Example 2: Using seek() in Binary Mode with Negative OffsetSuppose data.txt contains the following binary content:b'Code is like humor. When you have to explain it, its bad.' Python f = open("data.txt", "rb") f.seek(-10, 2) print(f.tell()) print(f.readline().decode('utf-8')) f.close() Output:47, its bad.Explanation:File is opened in binary mode ('rb').seek(-10, 2) moves 10 bytes before the end of the file.readline() reads from that point to the end.Output is decoded from binary to string.Refer the below article to understand the basics of File Handling. File Handling in Python. Reading and Writing to files in Python Create Quiz Comment S Soham_Lanke Follow 39 Improve S Soham_Lanke Follow 39 Improve Article Tags : Python python-file-handling Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like