Python basic text control file encoding format uses the file opening method commonly used by Python to read and write files

Posted by [n00b] on Thu, 24 Feb 2022 16:51:01 +0100

Coding format:

Common character encoding formats
The memory interpreter is Python
. py files are stored on disk using UTF-8 (external storage)

Reading and writing principle of documents:

1. File reading and writing is commonly known as "IO operation". I refers to input and O refers to output. Therefore, IO operation is input and output operation.
The principle of IO is a data structure called queue, which is a first in first out method.
2. Document reading and writing process
3. Operating principle:
Python operation file --- open or create a new file --- read and write files --- close resources

File read / write operations:

1. The built-in function open() creates a file object:
Synchronize the contents of the disk file with the contents of the objects in the program through the IO stream
2. Grammar rules:
file=open(filename[mode,encoding])

Meaning in grammar:

file object created
open() function to create a file object
filename = the name of the file to create or open
Mode open mode is read-only by default. Mode is to create a file or open a file. The write operation is w and the read operation is r
encoding: the default format of characters in text files is gbk

 
#Create a notepad file of a.txt at the location of E://python text, and enter China in the file. Then use Python to read the file
# Notepad file is gbk file encoding format by default, while python is UTF-8 encoding format by default, so it cannot be read directly
#file=open("E://python text/a.txt", "r",encoding="UTF-8")
#Open, open (input the location of the file in the first parameter position, read (r), or write (w) in the second parameter position, and encoding = "encoding format" in the third position), and then assign the result to file

file=open("E://Python text / a.txt "," R ", encoding =" UTF-8 ") #file is equal to a.txt file
print(file.readlines())    #file.readlines() reads the contents of the file and outputs it
file.close()     #Close file

Common file opening methods

File type:
According to the organization form of data in the file, the file is divided into the following two categories:
        1. Text file: it stores ordinary "character" text, which defaults to unicode character set and can be opened using Notepad program
        2. Binary file: the data content is stored in "bytes", which cannot be opened with Notepad, but must be opened with special software,
Examples: mp3 audio files, jop pictures, doc documents, etc

Opening method:
r) open the file in read-only mode, and the pointer of the file will be at the beginning of the file
w) open the file in write only mode. If the file does not exist, create it. If the file exists, overwrite the original content. The file pointer is at the beginning of the file. Note: w written content will replace the original content of the source file
A) open the file in the append mode. If the file does not exist, create it, and the file pointer is at the beginning of the file. If the file exists, append content at the end of the file, and the file pointer is at the end of the source file. Note: the content written by a will not replace the content of the source file, but will be appended and input to the back of the content of the source file.
b) open the file in binary mode, which cannot be used alone, but need to be used together with other modes, rb, or wb
+Open the file in read-write mode. It cannot be used alone. It needs to be used together with other modes.

#w. Open the file in write only mode. Each time the content is written, the original content in the file will be replaced.
hua=open("b.txt","w")   #If there is no b.txt file, create and modify the file, and then write the file
hua.write("Brother Hua is the most handsome!")   #If the file already exists, replace the original content of the original file with the written content. Because the original content will be replaced every time it is run, this is the only sentence when it is run again.
file.close()    #Close file
#a. The file is opened in the append mode. Each time the content is written, the input will be appended to the content of the source file, and the content of the source file will not be replaced.
zhi=open("b.txt","a")   #Open the file in the open mode of a, and the zhi variable is equal to the b.txt file
zhi.write("Dashuaibi")     #Because the opening method of a will not replace the contents of the source file, and will only append the write results to the original contents, there will be three big Shuai ratios in the file after running three times

file.close()


wei=open("E://python text/dazi.png","rb ") # open the initial daizi.png file in binary read-only mode
zhiwei=open("E://python text/da.png","wb ") # because there is no da.png file, create one and write it in binary mode
zhiwei.write(wei.read())      #read(), if a few bytes are not written or read in parentheses, then all bytes of the file will be read and returned. Here, the bytes in the file will be written to zhiwei after reading.

wei.close()    #Remember to close the file when finished
zhiwei.close()     #Remember to close the file when finished

Common methods for file objects:

read([size]) reads the contents of bytes or characters from the file and returns. If [size] is omitted, it will be read to the end of the file, that is, all contents of the file will be read at one time.
readline() reads a line from a text file
readlines() takes each line in the text file as an independent string object and returns these objects in a list
write(str) writes the contents of the string str to a file

writelines(s_list)_ List writes to the text file without adding line breaks

Seek (offset [, where]) moves the file pointer to a new position. Offset indicates the position relative to where:
offset: the parameter is positive to move towards the end direction, and the parameter is negative to move towards the start direction
Different values of where represent different meanings:
0: calculated from the file header (default)
1: calculate from the current position
2: calculated from the end of the document

tell() returns the current position of the file pointer
flush() writes the contents of the buffer to the file, but does not close the file
close() writes the contents of the buffer into the file, closes the file at the same time, and releases the resources related to the file object

Topics: Python Back-end