About the functions of the file
W Write mode
A append mode opens (starting with EOF, creating a new file if necessary)
R+ Open in read-write mode
w+ Open in read-write mode
A + opens in read-write mode
RB opens in binary read mode
WB opens in binary write mode (see W)
AB opens in binary append mode (see a)
Rb+ opens in binary read/write mode (see r+)
Wb+ opens in binary read/write mode (see w+)
Ab+ opens in binary read/write mode (see A +)
1File = Open ('Test.txt','W')2File.read ([size])#size is the length of the read, in bytes3File.readline ([size])#read a line, if a size is defined, it is possible to return only part of a row4File.readlines ([size])#take each line of the file as a member of a list and return to the list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file. 5File.write (str)#writes STR to a file, write () does not add a newline character after Str6File.writelines (seq)#write the contents of the SEQ to the file (multi-line write-once). This function simply writes faithfully and does not add anything behind each line. 7File.close ()#close the file. Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down. If a file is closed and then manipulated, it generates ValueError8File.flush ()#write the contents of the buffer to the hard disk9File.fileno ()#returns a "file label" for a long integer typeTenFile.isatty ()#whether the file is a terminal device file (Unix system) OneFile.tell ()#returns the current position of the file action tag, starting with the origin of the file AFile.next ()#returns the next line and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal. -File.seek (Offset[,whence])#moves the file-action marker to the location of offset. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, if the whence parameter is provided, whence can be calculated from scratch for 0, and 1 for the current position as its origin. 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made. -File.truncate ([size])#The file is cut to the specified size, the default is the location of the current file operation tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add. the - another way to open a file: -With open ('Test.txt','w+') as F: -F.write ('Test') + F.tell () -F.seek (100) +...
Common uses of the Python notes file