Python File method
The file object is created using the open function. The following table lists the commonly used functions of the file object:
Serial number | Method and description |
---|---|
1 | file.close() Close the file. After closing, the file can no longer be read or written. |
2 | file.flush() Refresh the internal buffer of the file and directly write the data in the internal buffer immediately into the file instead of passively waiting for the output buffer to be written. |
3 | file.fileno() Returns an integer file descriptor (file descriptor FD integer ), can be used in some low-level operations such as the read method of the os module. |
4 | file.isatty() Returns True if the file is connected to a terminal device, False otherwise. |
5 | file.next() Returns the next line of the file. |
6 | file.read([size]) Read the specified number of bytes from the file, if Not given or negative reads all. |
7 | file.readline([size]) Read the entire line, including the "\n" character . |
8 | file.readlines([sizehint]) Reads all lines and returns a list, if given sizeint>0, returns rows whose sum totals approximately sizeint bytes. The actual read value may be larger than sizhint because the buffer needs to be filled. |
9 | ##file.seek(offset[, whence])Set the current position of the file |
file.tell()Returns the current position of the file. | |
file.truncate([size])Intercept the file. The intercepted bytes are specified by size. Defaults to the current file location. | |
file.write(str)Write a string to the file, no return value. | |
file.writelines(sequence)Write a list of sequence strings to the file, wrapping if necessary You have to add newline characters to each line yourself. |