# - *- coding: utf-8-*-
# The test file name is:
# text.txt
# The content of the test file is:
# abcdefg
# Restore files after each operation
# r
# Open the file as read-only, the file is not writable
# An error will be reported 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','r')
# # FileNotFoundError:[Errno 2] No such file or directory:'test.txt'
# file =open('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, the file is not writable
# An error will be reported 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 reading and writing, write content as str
# The file pointer will be placed at the beginning of the file
# The rewritten content is replaced from the beginning
# 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 reading and writing, and the written content is bytes
# The file pointer will be placed at the beginning of the file
# The rewritten content is replaced from the beginning
# 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, and write content as str
# File is not readable
# If the file already exists, it will be overwritten and the original file content will be emptied
# If the file does not exist, create a new file
# file =open('test.txt','w')
# Create an empty file
# file =open('text.txt','w')
# file.write('gfedcba')
# file =open('text.txt','r')
# print(file.read())
# file.close()
# wb
# Open a file in binary format for writing only, and the writing content is bytes
# File is not readable
# If the file already exists, it will be overwritten and the original file content will be emptied
# If the file does not exist, create a new file
# file =open('test.txt','wb')
# Create an empty file
# file =open('text.txt','wb')
# file.write(b'gfedcba')
# file =open('text.txt','r')
# print(file.read())
# file.close()
# w+
# Open a file for reading and writing, write content as str
# If the file already exists, it will be overwritten and the original file content will be emptied
# If the file does not exist, create a new file
# file =open('test.txt','w+')
# Create an empty file
# file =open('text.txt','w+')
# file.write('gfedcba')
# file =open('text.txt','r')
# print(file.read())
# file.close()
# wb+
# Open a file in binary format for reading and writing, and the written content is bytes
# If the file already exists, 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 appending (write only), and write content as 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 after the existing content
# If the file does not exist, create a new file for writing
# file =open('test.txt','a')
# Create an empty file
# file =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 appending (write only), and the content is bytes
# If the file already exists, the file pointer will be placed at the end of the file, and the new content will be written after the existing content
# If the file does not exist, create a new file for writing
# file =open('test.txt','ab')
# Create an empty file
# file =open('text.txt','ab')
# file.write(b'aaa')
# file.close()
# file =open('text.txt')
# print(file.read())
# file.close()
# a+
# Open a file for appending (read and write), write content as 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 after the existing content
# If the file does not exist, create a new file for reading and writing
# file =open('test.txt','a+')
# Create an empty file
# file =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 appending (reading and writing), and the written content is bytes
# If the file already exists, the file pointer will be placed at the end of the file, and the new content will be written after the existing content
# If the file does not exist, create a new file for reading and writing
# file =open('text.txt','ab+')
# file.write(b'aaa')
# file.close()
# file =open('text.txt')
# print(file.read())
# file.close()
Recommended Posts