Python open read and write

# - *- 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

Python open read and write
Python file read and write operations
Python memory mapped file read and write method
How to read and write files with Python
Python and Go
Python write Tetris
Python introspection and reflection
[python] python2 and python3 under ubuntu
Python deconstruction and packaging
Write gui in python
Python3 configuration and entry.md
Python automated operation and maintenance 2
Python introduction and environment installation
Python know crawler and anti crawler
centos7 install python3 and ipython
ubuntu18.04 compile and install python3.8
CentOS7 install python3 and pip3
Python automated operation and maintenance 1
Python data structure and algorithm
Python multi-process and multi-thread basics
CentOS 6.9 compile and install python
CentOS 6 compile and install python 3
Generators and iterators in Python
Python write breakpoint download software
Python and js interactive call method
Python judges positive and negative numbers
python ftp upload files and folders
Use C++ to write Python3 extensions
Python crawler | Cognitive crawler request and response
How to write python configuration file
FM algorithm analysis and Python implementation
Common errors and solutions in python
Python implements string and number splicing
CentOS quickly install Python3 and pip3
How to write classes in python
Mongodb and python interaction of python crawler
How to read Excel in Python
Install Python3 and ansible under CentOS8
Python processing PDF and CDF examples
Played with stocks and learned Python
Configure python3 environment on centos7 and
How to write return in python
Python reads and writes json files
Python implements username and password verification
Install Python3 and Py under CentOS7
Python basic syntax and number types
Actual combat | Python write port scanner
Python learning os module and usage