What is the Python File seek () method? How do you understand and use it?

Source: Internet
Author: User
In this article, let's look at the python Seek Method, before we enter the article, we first need to know PythonseekWhat is it Seek ()It can be used in Python programming where it can play a role. These things we can all learn in today's article.

Overview

The Seek () method is used to move the file reading pointer to the specified location.

Grammar

The Seek () method syntax is as follows:

Fileobject.seek (offset[, whence])

Parameters

Offset--The starting shift, which represents the number of bytes that need to be shifted

Whence: Optional, default value is 0. Give the offset parameter a definition of where to start the offset, and 0 to start at the beginning of the file, 1 to start at the current position, and 2 for the end of the file.

return value

The function does not return a value.

Instance

The following example demonstrates the use of the ReadLine () method:

The contents of the file Runoob.txt are as follows:

1:www.runoob.com2:www.runoob.com3:www.runoob.com4:www.runoob.com5:www.runoob.com

To iterate through the contents of a file:

#!/usr/bin/python#-*-coding:utf-8-*-# Open File fo = open ("Runoob.txt", "rw+") print "file name:", Fo.nameline = Fo.readline () pri NT "read data:%s"% (line) # Reset file read pointer to the beginning Fo.seek (0, 0) line = fo.readline () print "Read data:%s"% (line) # Close file Fo.close ()

The result of the above example output is:

The file name is: Runoob.txt reads the data:  1:www.runoob.com reads: 1:www.runoob.com

In this article, we understand what is the Seek () method, do not understand the words in fact, they can try to do, after all, hands-on practice is the best way to verify the learning. Finally, I hope that this article will give you a little help in learning about Python.

For more information, please visit the PHP Chinese Web Python tutorial section.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.