To read and write files in Python and w + r + in the end what's the difference?

In fact, r is read-only and can only be read but not write, this is very clear, but can read and write r +, r + becomes too clear in the end after not add anything, or still can not write, do not have the experience of it, the following code, when read-only

f = open("test.txt", 'r', encoding="utf-8")  # 文件句柄
f.write("we are heros\n")

data = f.read()
print(data, type(data))
f.close()

In this case the error is obvious: io.UnsupportedOperation: not writable, writing should not be able to understand, but it becomes r +

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
f = open("test.txt", 'r+', encoding="utf-8")  # 文件句柄
f.write("we are heros\n")

data = f.read()
print(data, type(data))
f.close()

You tried to find nothing printed, while paying attention! ! ! There is no error. In the end what does it mean?

In fact, the problem is due to the mechanism of reading and writing, when a file is read or write a file in the class pointer is pointing to something at the end of the entire document, from the end of what started reading again, then definitely read all

Can not read, so there will be such a problem, and continuous f.read () twice, then the second reason nothing will read the same. How it finished then read it, see the code

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
 f = open("test.txt", 'r+', encoding="utf-8")  # 文件句柄
 f.write("we are heros\n")
 f.seek(0)
 data = f.read()
 print(data, type(data))
 f.close()

Plus f.seek (0) makes something like a pointer back to the beginning of the file can be read and then start from scratch. With f.tell () use English characters and returns a pointer to the location of that class (that is, the number of characters), but the Chinese word is the number of characters * 3, the specific reasons have to talk about heroes.

and w + r + the difference between what it can not be read can be understood as simply, a slightly different details of the!

  • r +: read before write words in the original text is added, because reading the class pointer is already at the very end, and if a first write-read, then scratch overlay writing (e.g., only modified the preceding character, back characters will not be deleted), class pointer at the end, not the end of the document is finished, you can read part uncovered written;

  • w +: to read-after-write, use f.seek (0) back to the initial position after the first finished and then began to read, if the first word is read can not read anything, because w + is sheer coverage written in the unused write operation before the document is completely blank, regardless of the file before there. so, it can only be read after write.

The difference between r and w, r this file must already exist and when w file may or may not have, if there is to be covered, else it does not create a (caution), r + writing is covered!

Guess you like

Origin blog.51cto.com/14246112/2461866