Byte mode of file object/b modeutf-8 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Read operation |
Write operation |
Pointer operation |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ASCII bytes |
Returnbytes/ byte typeAscii |
Write inbytes For example:b’This is ascii’ |
Useseek set any byte each time. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Chinese string |
Returnbytes/ byte type of garbled |
The string is encoded before it can be written. For example:‘‘.encode(‘utf-8’) |
Useseek Settings3
Text mode of file object Read operation Write operation Pointer operation ASCII bytes Returns a string that can be viewed Write to find string Useseek set any byte each time. Chinese string Returns a string that can be viewed Write to find string Useseek Settings3 Pattern File existence file does not exist operation Document content r Establishopen objects normally open normally. Establishopen object failed to read the original file. read-only Readable and unreadable, default isr You can use pointer to adjust position to read. w EstablishopenThe contents of the original document are directly emptied. Establishopen objects create new files normally Only write When the file is opened, the content is emptied. You can adjust the pointer position to write. a Establishopen objects normally open normally. Establishopen objects create new files normally Only write Does not cover the contents of the original document. Content can only be added at the end, and the pointer can not change the location of the write. If the content is not written, the original document will remain unchanged. r+ Establishopen objects normally open normally. Establishopen object failed to read the original file. Read and write Does not cover the contents of the original document. The pointer appends the content at the end of the default, and the pointer overwrites the contents of the original file when it writes in other locations. If the content is not written, the original document will remain unchanged. Read files according to pointer location w+ EstablishopenThe contents of the original document are directly emptied. Establishopen objects create new files normally Read and write When the file is opened, the content is emptied. The write operation covers the contents of the previous file. Read files according to pointer location a+ Establishopen objects normally open normally. Establishopen objects create new files normally Read and write At the end of the pointer, the file can only be added to the file and read the file according to the pointer position. If the content is not written, the original document will remain unchanged. Note: as long as it is a writable mode, it can be called multiple times before closing the file.writeDetailed explanation 1. Operation of documents The operation file is about the process, whether the file exists, open the file, read and write the file, close the file. python3 open file See/windowspycharm isutf-8staypycharmutf-8 And then runpycharm program, read this text file will be wrong, it will use.windows DefaultGBK code reads. Open the file f = open(‘musictxt’,encoding=’utf-8′)#encoding=’utf-8′ def open(file, mode=’r’, buffering=None, encoding=None, errors=None, newline=None, closefd=True): Open file modeA more detailed explanation is given in the table above. r read mode (default). w write mode only. a additional mode. Usually, files are opened by text, and strings of read and write files will be encoded in specific ways.UTF-8. Mode plus‘b’ read and write files are read and written in bytes. CommonJPGEXEUnix\n , Windows\r\n
If the file is read, the pointer position is not set. The default read file location pointer moves from the file header to the end of the file, so it cannot be read repeatedly after reading once. read file f.read(size)size > is an optional item, specifying the length of the string.size no specified or negative number will read the entire file. When the file size is two times the memory of the current machine, it will go wrong. If the end of the file is displayed, an empty string will be displayed. read(),readline()
Read line f.readline()‘\n’”
If you want to read all the lines in a file to a list, you can also use it.list(f) f.readlines() Write a document f.write(string)string is written to the file, and the length of the written character is returned.: >>> f.write(‘This is a test\n’) 15 To write other non string content, first convert it to string. >>> value = (‘the answer’, 42) >>> s = str(value) >>> f.write(s) 18 Pointer operation of file objects f.tell() method returns an integer indicating the current pointer position. f.seek(offset,from_what) method set the pointer position. form_what > is the starting position. The value is0 starts at the beginning of the document.1 represents the beginning of the current pointer position.2 indicates the beginning of the end of the document. You can manipulate this article, but notice that a string is 3 bytes. The pointer is in bytes, not 3. Note: When both Chinese and ASCII are included in the text, it is more troublesome to generate scrambling code whether byte or string operation pointer. For example, the following is produced.3 needs to be modified.bytes byte data to repair. operationbytes type characters, cut out bad data by slice method. >>> b b’abcdefg,\x8d\xe9\x9d\xa2\xe4\xba\x86,\x80\xe4\xb8\x8b\xe5\x9c\xa8\xe5\x89\x8d\xe9\x9d\xa2\xe5\x8a\xa0\xe4\xb8\x80\xe4\xb8\x8b,,try,try12345678abcdef——-hehe’ >>> b[:4] b’abcd’ >>> b=b[:8]+b[10:] >>> c.decode(‘utf-8’) ‘,,try,try12345678abcdef——-hehe’ >>> The following is a detailed example of file operation: According to the original content of python3 official manual, if you need to reprint, please indicate the source. |