Use of the seek () method for file operations in Python

Source: Internet
Author: User

Use of the seek () method for file operations in Python

This article mainly introduces how to use the seek () method for file operations in Python. It is the basic knowledge of getting started with Python. For more information, see

The seek () method sets the current position of the file at the offset. The parameter is optional. The default value is 0, which means absolute file location. If the value is 1, this means that the request is relative to the current location, and 2 indicates relative to the end of the file.

No return value. Note that if the file is opened or append with 'A' or 'a + ', any seek () operation will be undone in the next write operation.

If the file is opened only in the append mode of "a", this method is essentially an empty operation, but the read enable (mode 'a + '), it is still very useful for files opened in append mode.

If the file uses "t" in text mode, only the offset opened by tell () is valid. Other offsets may cause uncertain behavior.

Note that not all file objects are searchable.

Syntax

The syntax of the seek () method is as follows:

?

1

FileObject. seek (offset [, whence])

Parameters

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

Whence -- this is optional. The default value is 0, which means absolute file location. The other value is 1, which means seeking relative to the current location. 2 indicates relative to the end of the file.

Return Value

This method does not return any value.

Example

The following example shows how to use the seek () method.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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

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

?

1

2

3

4

Name of the file: foo.txt

Read Line: This is 1st line

 

Read Line: This

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.