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.