TXT plain text operation

TXT plain text operation



foreword

The operation of saving data to TXT text is very simple, and TXT text is compatible with almost any platform, but this has the disadvantage that it is not conducive to retrieval.


1. The idea of ​​​​operating TXT text

Use the open method provided by Python to open a text file and get a file operation object, which is assigned as file here. When extracting some information, use the write method of the file object to write the extracted content into the file.
After all the extraction is completed, finally call the close method to close it, and the content can be successfully written into the text.

In fact, it is the usage of the three methods of open, write, and close.

The first parameter of the open method is the name of the target file to be saved; the
second parameter is w, which means writing text by overwriting;
in addition, we also specify the encoding of the file as utf-8.
Finally, after the writing is complete, the close method needs to be called to close the file object.

2. Opening method----open

The second parameter of the open method is set to w, so that the source file will be cleared every time the text is written, and then new content will be written to the file, which is a way to open the file. There are actually several other ways to open files, here is a brief introduction.

parameter effect
r Open the file in read-only mode, which means that the content of the file can only be read, but not written. This is the default mode.
rb Open a file in binary read-only mode, usually used to open binary files, such as audio, picture, video, etc.
r+ Open a file in read-write mode, both for reading and writing.
rb+ Opening a file in binary read-write mode can also be read and written, but both read and write are binary data.
w Open a file for writing. If the file already exists, it will be overwritten. If the file does not exist, a new file is created.
wb Open a file for binary writing. If the file already exists, it will be overwritten. If the file does not exist, a new file is created.
w+ Open a file for reading and writing. If the file already exists, it will be overwritten. If the file does not exist, a new file is created.
wb+ Open a file in binary read-write format. If the file already exists, it will be overwritten. If the file does not exist, a new file is created.
a Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.
ab Open a file for binary append. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.
a+ Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode. If the file does not exist, a new file is created for reading and writing.
ab+ Open a file for binary append. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, a new file is created for reading and writing.

2. Code

Using the with as syntax. At the end of the with control block, the file is automatically closed, so there is no need to call the close method.

with open('game_data.txt', 'a',encoding='utf-8') as txtfile:
    txtfile.write(f'要写入文本的内容')

Summarize

This article only briefly introduces the use of open

Guess you like

Origin blog.csdn.net/weixin_45688123/article/details/125468803