In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about the difference between w + and r + in Python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
In fact, r is read-only, can only read but not write, this is very clear, but r + is read and write, after it becomes r +, I still don't know what to add, but I still can't write it. Is there such an experience? the following code is read-only.
In this case, the error report is obvious: io.UnsupportedOperation: not writable, unwritable should be able to understand, but become r +
If you try, you will find that nothing has been printed out, and pay attention at the same time! And I didn't report it wrong. What exactly does it mean?
In fact, it is due to the reading and writing mechanism. When a file is read or written once, the thing of the class pointer in the file points to the end of the whole file. If you start to read it again, it must be everything.
I can't read it, so there will be such a problem, for the same reason that if I f.read () twice in a row, I won't read anything the second time. So how do you finish writing and read on? see the code.
Add f.seek (0) to make the class pointer go back to the beginning of the file and read it from the beginning. With f.tell (), English and characters return the position of the pointer of that class (that is, the number of characters), but in Chinese it is the number of characters * 3, and you have to ask the hero to explain the specific reasons.
What is the difference between r + and w +? it can not be simply understood as both reading and writing. The details are slightly different!
Ritual: read before write is added after the original text, because the class pointer is already at the end after reading. If it is written first and then read, it is overwritten from the beginning (for example, only the previous characters are modified, and the subsequent characters will not be deleted). The class pointer stays at the end of the writing, not the end of the document, and you can read out the parts that have not been overwritten.
To write first and then read, first write and then use f.seek (0) to go back to the original position and then start reading. If you read first, you can't read anything, because w + is also a pure overwrite, and the document is completely blank before using the write operation, no matter what was in the file before. So, you can only write before you read.
The difference between r and w, r must already exist in this file and w can have or not, if is overwritten, else is not created (with caution), r + write is also overwritten!
These are all the contents of this article entitled "what's the difference between w + and r + in Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.