The following steps are generally experienced when manipulating files:
- Open File
- Manipulating files
1. Open the file:
File handle = open ('/path/to/file_name ', ' pattern ')
Pattern
- R: Read-only mode, which defaults to read-only mode when opening a file, and the action pointer at the beginning of the file.
- W: Write-only mode, the file does not exist, it is created, the original content is emptied, and the action pointer is at the beginning of the file.
- X: Write-only mode, the file does not exist, it is created, there is a report; The action pointer is at the beginning of the file.
- A: Append, write only, the file does not exist then create, exist in the original file append content, the action pointer to the end of the file.
+ indicates both read and write files:
- r+: Read and Write
- w+: Write Read
- x+: Write Read
- A +: Additional Reading
B means to operate in byte mode:
- RB: Open in bytes, read and write
- WB: Open in bytes, write read
- XB: Opened in bytes, read-write
- AB: Open in bytes, append read
NOTICE: When opened in B, the data read is byte type, and the data written is also of type Byte.
Through the With management context:
With open ('/path/to/file_name ', ' pattern ') as file_name:
Pass
With support for managing multiple contexts at python2.7 and beyond
With open ('/path/to/file_name1 ', ' pattern ') as File_name1,open ('/path/to/file_name2 ', ' pattern ') as file_name2:
Pass
Python file read and write operations