The processing mode of file processing and its black magic

Contents of this section


 

1. File opening format and encoding problem

2. File processing mode

3. The black magic of files

4. How to move the file cursor

 

 

1. File opening format and encoding problem


File opening format: read-only mode is the default without the specified mode. Special attention needs to be paid to the specified encoding. If not specified, the open function defaults to the system encoding. The system encoding is the default Windows system encoding if you are a Windows system.

If the file is opened, it will be closed. If it is only opened but not closed, it will occupy memory.

f = open( ' file name ' , encoding = ' utf-8 ' ) #f is 'file variable'  
f.close()    

The above opening method is troublesome, there is another way and it can open multiple files at the same time without automatically closing the file

with open ( ' filename ' , ' mode ' , encoding = ' utf-8 ' ) as file variable:
    File handling operations...

 

 

2. File processing mode

 

model describe
r Open the file as read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
rb Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. Generally used for non-text files such as pictures.
r+ Open a file for reading and writing. The file pointer will be placed at the beginning of the file.
rb+ Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as pictures.
w Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb Open a file in binary format for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file. Generally used for non-text files such as pictures.
w+ Open a file for reading and writing. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb+ Open a file in binary format for reading and writing. Overwrite the file if it already exists. If the file does not exist, create a new file. Generally used for non-text files such as pictures.
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, create a new file for writing.
away Open a file in binary format 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, create a new file 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, create a new file for reading and writing.
ab+ Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing.

 

 

3. The black magic of files

 

The file object file_obj has built-in methods for operating files. Commonly used read and write methods are:

method name meaning
file_obj.read() By default, all contents of the file are read.
file_obj.readline() Reads one line by default.
file_obj.readlines() By default, all lines of the file are read and returned as a list.
file_obj.write(s) Write the content s.
file_obj.writelines(lines) Writes all elements in the sequence lines.

 

4. How to move the file cursor

Each file object maintains a file pointer internally. When the file is opened in r or w mode, the pointer points to the beginning of the file, and when the file is opened in a mode, the pointer points to the end of the file. Read and write operations are based on the position of the previous pointer. offset.

We can also file_obj.seek(offset,whence)control the offset of the pointer using methods:

  • offset represents the offset in bytes.
  • The default value of whence is 0, which means the start of the file is the reference.
  • whence=1 means take the current position as the reference.
  • where=2 means take the end of the file as the reference (reverse order)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325260390&siteId=291194637