Supplement various Python File (file) open mode pits

1.r+

Official document: Open a file for reading and writing. The file pointer will be placed at the beginning of the file.

r+ The mode has both "read" & "write", but it does not give the positioning of "read" & "write" separately. In fact, the latter sentence is the positioning of "read", not the positioning of "write", causing a pit.

** So the specific definition should be:

  • " r+Mode" has both "read" & "write", when "read operation", the pointer points to the initial bit of the file "seek (0)"
  • When 'write operation', the pointer points to the end of the file "seek (0,2) is the next bit of the last non-null character"

Therefore, in fact, " r+write" in " amode" is equivalent to " mode" that is added at the end of the document.
So " r+mode" == r+ a**
PS. Using seek (n) positioning can force the content to be written at the target position.


2.w+

After open (file, 'w +'), directly clear the original text, it is impossible to use seek () to locate.

Guess you like

Origin www.cnblogs.com/deepblue775737449/p/12758139.html
Recommended