Correct understanding of the reading and writing modes of Python files w +, a +, and r +

Source: Internet
Author: User

The difference between w + and r + is not hard to understand. There is also a +
+ Read and write at the same time, you can read and write at the same time, while writing at the same time, while reading and writing at the same time, without flush, can be measured using seek and tell.

Fp = open ("a.txt", "a +", 0) print 'open', fp. tell () x = fp. read () print 'open read () ', fp. tell () print xfp. write ("123456 \ n") print 'write 1-6 ', fp. tell () x = fp. read () print "first read \ n", xfp. seek (0) x = fp. read () print "second read \ n", x, fp. tell () fp. close ()
The first running result is:
Open 0 open read () 0 write 1-6 8 first readsecond read1234568
The result of the second run is
Open 0 open read () 8123456 write 1-6 16 first readsecond read12345612345616

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.