Read and write errors in Python file operation

Source: Internet
Author: User

Error Restore:

Read error

In the process of reading the file, trying to read the data in the TXT file according to the size of 4 bytes (1 points) in sequence, reading and processing, can read to Ae99 1a41, the data is no longer continuous, 1a can not read, baffled its solution, a variety of attempts still helpless, There was a suspicion that Python had a bug for file operations.

The code is as follows:

Uartfile = open ('addata0.txt','r'= Uartfile.read (4)

Write error

The same error occurs when writing to a file, and cannot be written continuously when encountering 0a0d.

Basic knowledge:

There are two modes of file read and write operations: text mode and binary mode.

All files stored in the computer are rendered in the physical layer with binary 0 or 1 encoding, so the text pattern of the same file is the same as the binary mode bottom binary, except that the encoding is different on the upper rendering mode.

  Text patterns are character-encoded files, and common encodings are ASCII-encoded, Unicode-encoded, and so on.

Binary mode is a file based on value encoding.

Text files are basically fixed-length encoded (there are also non-fixed-length encodings such as UTF-8), each character is fixed in the specific encoding, ASCII code is 8 bits of encoding, Unicode generally accounted for 16 bits. Binary files can be considered as variable-length encodings, because the size of the numeric value is related to the length of the number of digits.

There are several main differences between the binary mode and the text mode of the current contact:

1. Line break in Windows text is \ r \ n (0x0D0A, carriage return line), in the Linux text is \ n (0x0a line break).

2. Terminator (0x1A), there is no terminator phenomenon in text-to-read text files under Linux, but this problem is encountered under Windows.

How to resolve:

The following pattern exists for Python text operations:

     r Read text mode

RB Read binary mode

     w Write text mode

     WB Write binary mode

Text manipulation of text files (reading or writing characters), directly using R or W.

Binary operation of the text file (read out or write data), using RB or WB.

The modification of your project resolves the read and write issues as follows:

Uartfile = open ('addata0.txt','rb'= open ('  channl1.txt','wb')

Read and write errors in Python file operation

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.