How to use the seek() method to operate files in Python

不言
Release: 2018-04-28 14:18:01
Original
4738 people have browsed it

This article mainly introduces the tutorial for using the seek() method to operate files in Python. It is the basic knowledge for introductory learning of Python. Friends who need it can refer to it

Seek() method in partial Move sets the current location of the file. The parameter is optional and defaults to 0, which means absolute file positioning, if its value is 1, it means seeking relative to the current position, and 2 means relative to the end of the file.

No return value. Note that if the file is opened or appended using 'a' or 'A', any seek() operation will be undone on the next write.

If the file is only open for writing using 'a' append mode, this method is essentially a no-op, but with read enabled (mode 'a'), it still opens the file in append mode very it works.

If the file uses "t" in text mode, only the offset returned by tell() is legal. Using other offsets can result in undefined behavior.

Please note that not all file objects are searchable.
Syntax

The following is the syntax of the seek() method:


fileObject.seek(offset[, whence])
Copy after login


Parameters

  • Offset -- This is the position of the read/write pointer in the file.

  • whence -- This is optional, defaults to 0, which means absolute file positioning, other values are 1, which means seeking relative to the current position, 2 means Relative to the end of the file.

Return value

This method does not return any value.
Example

The following example shows the use of the seek() method.


#!/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()
Copy after login


When we run the above program, it will produce the following results:


Name of the file: foo.txt Read Line: This is 1st line Read Line: This
Copy after login


Related recommendations:

Python study notes-open() function opens the file path and reports an error



The above is the detailed content of How to use the seek() method to operate files in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!