File built-in function: the open () function provides a common interface for initializing input/output operations
open () basic syntax:File_object = open (filename,access_mode= ' R ', buffering=-1) filename is a string that contains the name of the file to be opened (can be a relative or absolute path) Access_mode is also a string , usually ' R ', ' W ', ' A ' for read, write, and append, the file opened using ' R ' mode must already exist. ' W ' mode open files are emptied prior to re-
Factory function file ():As with open (), you can replace the built-in methods with each other: file methods are divided into four categories: input, output, intra-file move, and miscellaneous operations
Input:1, read (): Read bytes into a string, there is an optional parameter size, the default is-1, if 1 or a complex number then the file will be read to the end of the file. 2, ReadLine (): Reads a line of the file, including the line terminator. The same as read () also has an optional parameter size. 3, ReadLines (): Read all (remaining) and then return them as a list of strings, it has an optional parameter sizhint represents the maximum word size returned.
Output:1. Write (): writes a string containing text data or binary data blocks to a file. 2, Writelines (): For the list operation, accept a list of strings as parameters, write them to the file, the line terminator is not automatically added, if necessary, you must call the Writelines method before the end of each line to add a terminator. When reading rows from a file using read () or readlines (), Python does not delete the line terminator
moving within a file:The Seek () method can move a file pointer to a different location in a file
Syntax:
Fileobject.seek (offset[, whence])
Parameters: The following is the detailed parameter: offset: The location of the read/write pointer to the file. Whence: This is optional, the default is 0, which means absolute file positioning, the other value is 1, which means that the current position and 2 means seek relative to the end of the file.
file iterations:For Eachline in F:
file Close:F.close ()
built-in methods:
file Built-in properties:
Python core programming (Chapter Nineth)-File and input/output