One, open mode of the file
Open mode |
Description |
R |
Read-only mode open, default |
W |
Write-only opening, written in append form, if file is directly overwritten, if there is no file created |
A |
Write-only open, if the file exists append content to the end, if no file exists |
+ |
All modes can be added, indicating a readable and writable |
Ab |
Binary write-only mode open, if the file exists append to the end, if no file exists |
Rb |
Binary mode Open |
Wb |
Binary write, if the file exists directly overwrite, if there is no file created |
Second, the operation of the file
1,f.close () Close file
2,f.write (b) Write the content, note that if the W mode means writing with a string, WB represents a binary write.
3,f.closed returns True if the file is closed, false otherwise.
4,f.fileno () returns the file descriptor.
5,f.flush () Refreshes the internal buffer of the file, directly writes the data of the internal buffer to the file immediately, instead of passively waiting for the output buffer to be written.
6,f.encoding the encoded format of the returned file
7,f.readline ([size]) if size does not read the data one line at a time, if there is a size, the size character is read out
8,f.readlines () reads all the files, returns the list
9,f.tell () returns the position of the file pointer
10,f. seek(offset[, whence]) sets the current bit position of the file pointer, Offset --The starting shift, which is the number of bytes required to move the offset,whence: optional, the default value is 0. Give the offset parameter a definition of where to start the offset, and 0 to start at the beginning of the file, 1 to start at the current position, and 2 for the end of the file.
11,f.name Return file name
f.seekable () file cursor is movable
13,f.readable () file is readable
14,truncate
14,f.writeable () file is writable
15,f.read ([size]) reads a file of size bytes, such as size without or negative, reading the entire content.
16,f.truncate (size) intercepts a size character, starting at the cursor by default.
17,f.writelines (Sequence) writes a file, which is an iterative object that can return a string or binary
Third, read the file
for in F.readlines (): #返回成列表后读取
for in F: #f为迭代器, when the file is larger, this is better
Python's operations on files