Python seek() Method
Python seek() method is used for changing the current location of the file handle. The file handle is like a cursor, which is used for defining the location of the data which has to be read or written in the file.
Offset: This is used for defining the number of positions to move forward.
from_where: This is used for defining the point of reference.
NOTE: The seek() method in Python does not return any value.
The from_where argument is used for selecting the reference point. It can accept three values:
The from_where argument is set to 0 by default. Therefore, the reference point cannot be set to the current position or end position, in the text mode, except when the offset is equal to 0.
Example1: (The user has to read the text file, which contains the following text:
"This is the sentence I am Writing to show the example of the seek() method working in Python."
30 ing to show the example of the seek() method working in Python.
Sample Text File -
Example 2: (method 2)
Name of the file: text.txt Read Line: This is the sentence I am Writing to show the example of the seek() method working in Python. Read Line: The line no. 2 for showing the second method of writing the seek() method is python
The seek() method can also work with the negative offset, but only when the file is opened in binary mode.
Example 3: (negative offset)
25 Writing to show the example of the seek() method working in Python.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week