#-*-coding:utf-8-*-# test file is named: # text.txt# Test file content is: # abcdefg# after each operation to restore the file # r# read-only open the file, the file is not writable # to open the file does not exist when the error # file will be placed in the file's pointer Start # This is the default mode # # file = open (' Test.txt ', ' R ') # # Filenotfounderror: [Errno 2] No such file or directory: ' test.txt ' # file = O Pen (' Text.txt ', ' R ') # Print (File.read ()) # # abcdefg# File.write (' aaa ') # # IO. Unsupportedoperation:not writable# File.close () # rb# open a file in binary format for read-only, file not writable # An error will be made when the file to be opened does not exist # The file pointer will be placed at the beginning of the file # This is the default mode # # File = open (' Test.txt ', ' RB ') # # Filenotfounderror: [Errno 2] No such file or directory: ' test.txt ' # file = open (' Text.txt ', ' RB ') # Print (File.read ()) # B ' ABCDEFG ' # # File.write (b ' AAA ') # # IO. Unsupportedoperation:not writable# File.close () # r+# open a file for read-write, the write content for the str# file pointer will be placed at the beginning of the file # re-write the content from the beginning to replace # file = Open (' Text.txt ', ' r+ ') # file.write (' aaa ') # file.close () # file = open (' Text.txt ', ' R ') # Print (File.read ()) # # ' ABCDEFG ' # File.close () # rb+# open a file in binary format for read-write, write content to bytes# file pointer will be placed at the beginning of the file # re-write the content from scratch Replace # file = open (' Text.txt ', ' rb+ ') # # File.write (' aaa ') # # TypeerroR:a Bytes-like object is required, not ' str ' # file.write (b ' AAA ') # file.close () # file = open (' Text.txt ', ' RB ') # print (file. Read ()) # # B ' AAADEFG ' # file.close () # w# open a file for writing only, write to the str# file is unreadable # If the file already exists then overwrite it, the original file contents will be emptied # if the file does not exist, create a new file = open ( ' Test.txt ', ' W ') # Create an empty file # files = open (' Text.txt ', ' W ') # file.write (' GFEDCBA ') # file = open (' Text.txt ', ' R ') # print (FILE.R EAD () # file.close () # wb# open a file in binary format only for writing, write to bytes# file is unreadable # If the file already exists then overwrite it, the original file contents will be emptied # if the file does not exist, create a new File # = open (' Test.txt ', ' WB ') # Create an empty file # filename = open (' Text.txt ', ' WB ') # File.write (b ' GFEDCBA ') # file = open (' Text.txt ', ' R ') # print (file . read () # file.close () # w+# open a file for read-write, write to str# if the file already exists then overwrite it, the original file content will be emptied # if the file does not exist, create a new File # = open (' Test.txt ', ' w+ ') # Creating an empty File # file = open (' Text.txt ', ' w+ ') # file.write (' GFEDCBA ') # file = open (' Text.txt ', ' R ') # Print (File.read ()) # File.clo Se () # wb+# open a file in binary format for read-write, write content bytes# if the file already exists then overwrite it # if the file does not exist, create a new file # [file = open (' Text.txt ', ' wb+ ') # File.write (b ') GFEDCBA ') # file = open (' Text.txt ', ' R ') # Print (File.read ()) # file.close () # a# open a file for append (write only), write to str# if the file already exists, the file pointer will be placed at the end of the file, the new content will be written to the existing content after # if the file does not exist, Create new file to write # file = open (' Test.txt ', ' a ') # Create an empty file # file = open (' Text.txt ', ' a ') # File.write (' aaa ') # file.close () # file = Op En (' text.txt ') # Print (File.read ()) # file.close () # ab# open a file in binary format for append (write-only), write to bytes# if the file already exists, the file pointer will be placed at the end of the file, The new content will be written to the existing content after # if the file does not exist, create a new file to write to it # file = open (' Test.txt ', ' AB ') # Create an empty file # filename = open (' Text.txt ', ' AB ') # File.write (b ' AAA ') # file.close () # file = open (' Text.txt ') # Print (File.read ()) # file.close () # a+# open a file for append (read-write), write to str# if the file already exists, The file pointer will be placed at the end of the file and the new content will be written to the existing content # if the file does not exist, create a new file to read and write # file = open (' Test.txt ', ' A + ') # Create an empty file # filename = open (' Text.txt ', ' A + ') # file.write (' aaa ') # file.close () # file = open (' Text.txt ') # Print (File.read ()) # file.close () # ab+# Open a file in binary format for append (read-write), write to bytes# if the file already exists, the file pointer will be placed at the end of the file, the new content will be written to the existing content after # if the file does not exist, create a new file for read-write # file = open (' Text.txt ', ' ab+ ') # File.write (b ' AAA ') # file.close () # file = open (' Text.txt ') # print (file. read ()) # File.close ()
Refer to Python open for a summary of read, write, append
Python Open Read and write