In our previous study, no matter which data type operation we learned, the data we used when testing the program was not saved. If we want to statistically analyze the correlation of the data, then we need to save the data to In local files, Python provides the operations of accessing files, accessing directories, reading files, and writing files. Python's file objects are also called similar file objects or streams. In this section, we first learn about file operations.
We need to use the open() function when creating or opening a file. Its syntax structure is:
file=open(filename,mode='r',buffering=-1,encoding=None,errors=None,newline=None,closefd=True,openr=None)
Among these parameters, we commonly use filname and mode. You can have a brief understanding of the rest of the parameters.
filename is the file to be created or opened, using single quotes or double quotes. If it is in the same folder as the current file, you can directly write the name of the file. If it is not there, enter the absolute path.
mode is the parameter setting mode, which specifies the file opening method. The default opening method is read-only (r). Commonly used mode parameters include r, rb, wb, xb, ab and rt, wt, xt, and at, which correspond to For binary files and text files, t can be omitted when used.
Optional parameter, you can set the buffer mode. If it is -1, the buffer will be automatically set, usually 4096 or 8192 bytes. If it is 0, the buffer will be turned off and written directly to the file.
The former is the encoding method for opening the file, and the suffix specifies the processing method when encoding errors occur.
Line break mode.
Used when describing files.
Used when describing files.
Let's create a file through an example.
file=open('test.txt','w')file.write('first write')file=open('test.txt','r+')file.write('second write overwrite first write')
At this point we can find that there is an additional test.txt file in the current directory, and we open it.
We can find that the content in the text is what we wrote for the second time, because the first time we created the file, and then the second time we opened the file again, the opening mode was overwriteable, and the second time we The written content also covers the content we wrote for the first time. The following table lists the commonly used opening methods.
Let's take a look at append mode through an example.
The code is as follows:
file=open('test.txt','w')file.write('First write.')print(file)file=open('test.txt','a+')file.write(' Second append writing. ')print(file)
The content of the file is:
First write. Second additional write.
The output result is in binary format:
<_io.TextIOWrappername='test.txt'mode='w'encoding='cp936'><_io.TextIOWrappername='test.txt'mode='a+'encoding='cp936'>
We can need to close the file after writing to ensure that the content in the file is not modified by malicious plug-ins. The closing method is:
file.close()
Just add it at the end of the file.
Regarding the creation and opening of files, we must pay attention to how it is opened or created. This is very important for our subsequent file operations. In the next section we will learn how to read files.