File opening Mode
Open Mode |
Perform operations |
'R' |
Open a file in read-only mode (default) |
'W' |
Opening the file as a write will overwrite the existing file |
'X' |
If the file already exists, opening in this mode will cause an exception. |
'A' |
Open in write mode. If the file exists, append it to the end. |
'B' |
Open a file in binary mode |
'T' |
Open in text mode (default) |
'+' |
Read/write mode (can be added to other modes) |
'U' |
Support for common line breaks |
File object Method
File object Method |
Perform operations |
F. Close () |
Close file |
F. Read ([size =-1]) |
Read size characters from the file. When no size is specified or a negative value is given, read all the remaining characters and return them as strings. |
F. Readline ([size =-1]) |
Read from the file and return a row (including the row Terminator). If the size is defined, the return size is a string of characters. |
F. Write (STR) |
Write string STR to file |
F. writelines (SEQ) |
Writing string sequence seq to a file. seq should be an iteratable object that returns a string.
|
F. Seek (offset, from) |
Move the file pointer in the file, and offset the offset byte from (0 indicates the start position of the file, 1 indicates the current position, and 2 indicates the end of the file ). |
F. Tell () |
Returns the current position in the file. |
F. truncate ([size = file. Tell ()]) |
Truncates a file to the size byte. By default, it is truncated to the current position of the file pointer. |
17 file opening mode and file object method ()