Saturday, July 25, 2020

Python File seek() Method with example



Hello guys! Welcome back to another section of my tutorial on Python. In this tutorial guide, we are going to be discussing about Python File seek() method.

Python file seek() method sets the file's current position at the offset. The whence argument is optional and defaults to 0, which simply means absolute file positioning, other values are 1 that means seek relative to the current position and 2 means seek relative to the file's end.

There is no return value. Note that if the file is opened for appending using either 'a' or 'a+', any seek() operations will be undone at the next write.

If the file is only opened for writing in append mode using 'a', this method is essentially a no-op, but it remains useful for the files opened in append mode with reading enabled (mode 'a+').

If the file is opened in the text mode using 't', only offsets returned by tell() are legal. Use of other offsets causes undefined behavior.

Note that not all file objects are seekable.

Syntax

The following below is the syntax for Python File seek() method -

fileObject.seek( offset[,  whence] );


Parameter Details

  • offset - Position of the read/write pointer within the file.
  • whence - It is optional and defaults to 0, which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file's end.

Return Value

It does not return any value.

Example

The following below is a simple example -

Python is a great language
Python is a great language

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

line = fo.readline()
print "Read Line: %s" % (line)

# Again set the pointer to the beginning
fo.seek(0, 0)
line = fo.readline()
print "Read Line: %s" % (line)

# Close opend file
fo.close()

Output
When the above code is executed, it will produce the following result -

Name of the file:  foo.txt
Read Line: Python is a great language.

Read Line: Python is a great language.


Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Python File tell Method.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.