seek() function in Python Last Updated : 20 Jun, 2025 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us Next Article seek() function in Python S Soham_Lanke Follow Improve Article Tags : Python python-file-handling Practice Tags : python Similar Reads set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i 3 min read sum() function in Python The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. Pythonarr = [1, 5, 2] print(sum(arr))Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything list , tuples or dict 3 min read type() function in Python The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three argument types (object, bases, dict) are passed, it re 5 min read Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age) 3 min read Python len() Function The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example:Pythons = "GeeksforGeeks" # G 2 min read Like