Python Basics-File read/write ' R ' and ' RB ' differences

Source: Internet
Author: User

One, Python file read and write several modes:

R,RB,W,WB So when you read and write files, is there a major difference between the B logo and the one?

1. How to use the file to identify

' R ': The default value that represents reading data from a file.
' W ': Indicates that you want to write data to the file and truncate the previous content
' A ': indicates that you want to write data to the file and add it to the current content trailer
' r+ ': Indicates a read-write operation to the file (deleting all previous data)
' R+a ': Indicates that a file can be read and written (added to the end of the current file)
' B ': Indicates that the binary data is to be read and written

2, read the file to read the file operation, until read to the end of the document (EOF) to read to the file finally, Python will consider the byte \x1a (26) converted to the character is the document Terminator (EOF),

Therefore, when you use ' r ' to read binary files, there may be a phenomenon of incomplete document reading.

 

example:
      binaries have the following data from low to High: 7F 1 a 2F 3D 2C + 2E
Span style= "FONT-SIZE:14PX; font-family: "microsoft yahei" ">      if read with ' R ', read to the third byte, that is, the end of the file is considered.
      if using ' RB ' Reading by bits does not convert the bytes read into characters, thus avoiding the error above.


solution:
Span style= "FONT-SIZE:14PX; font-family: "microsoft yahei" ">      binaries read ' RB '

  Summary:
      use ' r ' when you encounter ' 0x1A ', it is considered as the end of the file, is EOF. There is no problem with ' RB ',


That is, if you read the file in binary writing and then use it, if there is ' 0x1A ', it will only read part of the file.
Using ' RB ' will always read the end of the file.


3, write the file for the string x= ' abc\ndef ', we can use Len (x) to get its length as 7,\n we call the newline character, is actually 0x0a. When we write with ' W ' as text, the ' 0x0A ' will automatically be changed to two characters ' 0x0D ', ' 0x0A ', i.e. the file length actually becomes 8 in the Windows platform. When read with the ' R ' text, it is automatically converted to the original newline character. If you replace it with a ' WB ' binary, it will keep a character intact and read it as it is. So if you write it in text and read it in binary mode, consider the extra byte. ' 0x0D ' is also called carriage return. Linux does not change, because Linux uses only ' 0X0A ' to represent line breaks.

Python Basics-File read/write ' R ' and ' RB ' differences

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.