This article is participating in Python Theme Month. See the link for details

file.close()

File.close () closes the file. The file cannot be read or written after being closed. (No argument, no return value)

Overview: The close() method is used to close an open file. The closed file cannot be read or written. Otherwise, ValueError is raised. The close() method allows multiple calls. When a file object is referenced to operate on another file, Python automatically closes the previous file object. It is a good practice to use the close() method to close files.

file.flush()

File.flush () flusses the internal file buffer, writing data directly to the file instead of passively waiting for the output buffer to write. (No argument, no return value)

Overview: The flush() method is used to flush the buffer by immediately writing data to the file while flushing the buffer, without passively waiting for the output buffer to write. Normally, the buffer is flushed automatically after the file is closed, but sometimes you need to flush it before closing, using the flush() method.

file.fileno()

File.fileno () returns an integer file descriptor (file Descriptor FD integer) that can be used for low-level operations such as the read method on OS modules. (No arguments, return value: return file descriptor.)

Overview: The fileno() method returns an integer file descriptor (file Descriptor FD integer) that can be used for I/O operations on the underlying operating system.

file.isatty()

File.isatty () Returns True if the file is connected to a terminal device, False otherwise. (No arguments, return value: True if connected to a terminal, False otherwise.)

Overview: The isatty() method checks whether a file is connected to a terminal device and returns True if it is, False otherwise.

file.next()

File.next () returns the next line of the file. (No argument, return value: return the next line of the file.)

Summary: File objects in Python 3 do not support the next() method. Python 3’s built-in function next() calls the next() method through an iterator to return the next item. Within the loop, the next() method is called each time through the loop, which returns the next line of the file and raises StopIteration if the end (EOF) is reached

file.read([size])

File.read ([size]) Reads the specified number of bytes from the file, or all if none is given or negative. (Takes size — the number of bytes read from the file, and returns bytes read from the string.)

Overview: The read() method is used to read a specified number of bytes from a file, or all if none is given or negative.

file.readline([size])

File.readline ([size]) reads the entire line, including the “\n” character. Size — the number of bytes read from the file. Has a return value: returns the bytes read from the string. Summary: The readline() method is used to read an entire line from a file, including the “\n” character. If a non-negative parameter is specified, the number of bytes of the specified size, including the “\n” character, is returned.

file.readlines([sizehint])

File.readlines ([sizeHint]) reads all lines and returns a list, with a total of approximately sizeint bytes if sizeint>0 is given. The actual read value may be larger than sizhint because the buffer needs to be populated. Sizehint — the number of bytes to read from the file. Has a return value: returns a list containing all rows.

Summary: The readlines() method is used to read all lines(up to the end character EOF) and return a list of lines that, given sizeint>0, sum to approximately sizeint bytes. The actual read value may be larger than sizhint because the buffer needs to be filled. An empty string is returned if the terminator EOF is encountered.

file.seek(offset[, whence])

File. seek(offset[, whence]) Sets the current location of the file (offset: the offset from which the file starts, that is, the number of bytes to be moved, whence: Optional. The default value is 0. Give the offset argument a definition that indicates where to start the offset; 0 indicates the start of the file, 1 indicates the start of the file, and 2 indicates the start of the file. No return value)

Overview: The seek() method is used to move the file read pointer to a specified position.

file.tell()

File.tell () returns the current location of the file. (No argument, return value: return the current location of the file.)

Summary: The tell() method returns the current location of the file, that is, the current location of the file pointer.

file.truncate([size])

Truncate ([size]) Truncates a file. Truncated bytes are specified by size. The default value is the current file location. (With argument: size — optional, if present truncate file to size bytes. No return value)

Overview: The truncate() method is used to truncate a file. If size is specified, the truncate file is size characters. If size is not specified, the current position is reset.

file.write(str)

File.write (STR) writes a string to a file with no return value. (With arguments: STR — the string to write to the file. . No return value)

Overview: The write() method is used to write the specified string to a file. The string content is stored in the buffer until the file is closed or the buffer is flushed, so you don’t see anything written to the file.

file.writelines(sequence)

File. Writelines (sequence) Writes a list of sequence strings to a file. If newlines are required, add newlines for each line. STR — the sequence of strings to be written to the file. . No return value)

The writelines() method is used to write a sequence of strings to a file. This sequence string can be generated by iterating objects, such as a list of strings. Newline requires the newline character \n.