What is python file targeting? What is the role of file positioning?

Source: Internet
Author: User
In the Python learning Session of this article today, let's look at python file positioning. In the next article I'll tell my friends what the file locator is in Python and what this file location can do.

File location

The tell () method tells you the current position within the file, in other words, the next read and write will occur after so many bytes at the beginning of the file.

The Seek (offset [, from]) method changes the position of the current file. The offset variable represents the number of bytes to move. The from variable specifies the reference position at which to begin moving bytes.

If from is set to 0, this means that the beginning of the file is used as the reference location for moving bytes. If set to 1, the current position is used as the reference location. If it is set to 2, then the end of the file will be used as the reference location.

Example:

#!/usr/bin/python#-*-coding:utf-8-*-# open a file fo = open ("Foo.txt", "r+") str = fo.read () print "Read the string is:", str# find the current bit Position = Fo.tell () print "Current file location:", position# reposition the pointer again to the beginning of the file position = Fo.seek (0, 0) str = fo.read (+) print "Reread string: ", str# close Open file Fo.close ()

The result of the above example output:

The string read is: The  current file location for TE:  10 re-read string: This is  te
Related Article

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.