x Write mode , Create a new file , An error is reported if the file already exists .
b Binary mode .
Open a file to update ( Can read but write ).
U Universal newline pattern ( Not recommended ).
r Open the file read-only . The pointer to the file will be placed at the beginning of the file . This is the default mode .
rb Open a file in binary format for read-only use . 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 only for writing . Open the file if it already exists , And edit from the beginning , The original content will be deleted . If the file does not exist , Create a new file .
wb Opening a file in binary format is only used for writing . Open the file if it already exists , And edit from the beginning , The original content will be deleted . 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 . Open the file if it already exists , And edit from the beginning , The original content will be deleted . If the file does not exist , Create a new file .
wb+ Open a file in binary format for reading and writing . Open the file if it already exists , And edit from the beginning , The original content will be deleted . 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 . in other words , The new content will be written after the existing content . If the file does not exist , Create a new file to write to .
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 . in other words , The new content will be written after the existing content . If the file does not exist , Create a new file to write to .
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 . Append mode when the file opens . 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 .
02 Read file contents
fl = open('HelloWorld.txt','r',encoding='utf-8')
file.read([size]) If not specified, return the whole file , If the file size >2 Double the memory , An empty string is returned when the end of the file is read .
file.readline() Back to the line .
file.readlines([size]) Returns a list of the specified number of rows , If not specified, all lines will be returned .
03 Write to the document
f.write(···) If you want to write data other than strings , First convert it to a string .
04 Get current pointer position ( Bytes to file header )
f.tell()
05 Move the pointer position
f.seek( Offset ,[ The starting position ]) The starting position : 0 - The file header , The default value is ; 1 - The current position ; 2 - End of document