File write and write cache for Python file processing (three)

Source: Internet
Author: User

Python's Open is written in the following ways:

Write (str): Write str to file

Writelines (Sequence of strings): writes multiple lines to a file with parameters as an iterative object

First look at the Writelines () method:

1 f = open ('blogCblog.txt'w# First create a file object, open with W2 f.writelines ('123456# write a file using the ReadLines () method          

After running the above results, you can see that the BlogCblog.txt file has 123456 content, it is important to note that mode is ' W ' pattern (write mode), and then look at the following code:

1 f = open ('blogCblog.txt'w# First create a file object, open with W # towrite to the file with the ReadLines () method  

A typeerror is reported after running the above code because the parameter passed in by Writelines is not an iterative object.

Then look at the Write method:

Can see in the cmd run above code, obviously write Blogcblog this string, but in BlogCblog.txt this file does not have blogcblog this string oh, that is, no write success, why? Then take a look at this piece of code:

This code wrote 10000 lines Blogcblog string, open blogCblog.txt file discovery, there is content, but only 9,000 lines, not 10000, how to write a blogcblog is not successful? In fact, the Python write process is to write a string into a call buffer, when you write a byte greater than or equal to the size of the buffer, it will be automatically written to the file, and when you write a byte smaller than the size of the buffer, there will always be in the buffer.

The solution to this problem is to call the close () method, first empty the contents of the BlogCblog.txt file, and then run the following code:

You will find the Blogcblog string in the BlogCblog.txt file, which is the credit of close ().

Similarly, when you write a data that is greater than or equal to the buffer size, even though no call to close () is written to part of the data, it is because the buffer does not fit so much data that it is automatically written to your file, and the remaining data is in the buffer because it is smaller than the size of the buffer. This is why when you write data that is greater than or equal to the size of the buffer, only part of it is written, and when you simply call the close () method, the rest of the buffer's data is written to the file.

So, when you're done with the file, don't forget to turn it off!!!

Also: If you do not close, you can delete this file, because your process is occupied! And when you open this file a lot of times does not close the words will open failure!

File write and write cache for Python file processing (three)

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.