First, open the file:
Syntax: File_obj=open (Filename,mode)
The open () function returns a file object where filename is the file name and if the file does not exist, a file is created, and mode indicates how the file is opened.
Common mode values:
| Mode |
Description |
| R |
Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode. |
| Rb |
Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. |
| r+ |
Open a file for read-write. The file pointer will be placed at the beginning of the file. |
| rb+ |
Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file. |
| W |
Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file. |
| Wb |
Open a file in binary format only for writing. Overwrite the file if it already exists. If the file does not exist, create a new file. |
| w+ |
Open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. |
| wb+ |
Opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. |
| A |
Opens 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 to the existing content. If the file does not exist, create a new file to write to. |
| Ab |
Opens 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 to the existing content. If the file does not exist, create a new file to write to. |
| A + |
Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write. |
| ab+ |
Opens 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 to read and write. |
Second, the file read
1. F.read ([size]) returns a string, size can optionally return a string of the specified length, without specifying the size by default, the entire file is read, where the size is set as an iterator.
2. F.readline () returns a string that reads a line in the story.
3. F.readlines () returns a list of each line string as an element.
Third, file write
F.write (str)--Writes a string that returns the number of characters written.
F.writelines (SEQ)--Writes a sequence to a file, such as a newline, which requires itself to write ' \ n '.
Iv. closing files
F.close ()--Closes the file, frees the memory, and then throws an exception when it is closed.
V. Other
F.tell () returns the location of the file object, counting the number of bytes from the beginning to the current position.
F.seek () Move file location:
1. Seek (x,0)-moves x characters from the first character position of the file.
2. Seek (x,1)-moves x characters from the current position of the file.
3.seek (x,2)-moves x characters from the end of the file.
Note: X can be negative, for example seek ( -3,2) is the third last character
F.flush ()--refreshes the buffer of the file, writes the buffer data to the file, and empties the buffer.
F.fileno ()--Returns the file descriptor.
F.isatty ()--detects if the device is connected to the terminal.
F.truncate ([size])--truncate file, not filled size indicates that a file is truncated from the current position to the end of the file, and if size is set, it is truncated to a file from the set character position to the end.
Python file processing