![]() |
VOOZH | about |
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:
This will move the file cursor to position 10 and begin reading from there.
file.seek(offset, from_what)
Parameters:
Note: In text mode, from_what can only be 0. Using 1 or 2 requires binary mode ('rb').
Returns: new absolute cursor position from the beginning.
Let's say GfG.txt contains:
Code is like humor. When you have to explain it, it’s bad.
Output:
20
When you have to explain it, it’s bad.
Explanation:
Suppose data.txt contains the following binary content:
b'Code is like humor. When you have to explain it, its bad.'
Output:
47
, its bad.
Explanation:
Refer the below article to understand the basics of File Handling.