File read/write
One, open and close files
The first thing to do is to open a file to create the file object before you can read and write to it.
#1. Open: Use the Open function
#2. Close: Use the Close function: Flushes any information that has not been written in the buffer, closes the file, and then cannot read and write to the file.
# Flush Method Flush buffer can also be called before close
# fil = open (filename, open file mode, buffer), where the following two parameters can be omitted, the pattern has read (R), write (W), append (a), etc.
#缓冲区 (equals 0: does not register, equals 1: One row, greater than 1: Cache size of the storage area, less than 0: system default buffer size)
Second, read the file
After opening a file in ' R ' mode, you can call the Read function to read all the contents of the file at once, or you can specify how many bytes to read at a time.
Example:
1FIL1 = open ('Yyc.txt','R',-1)#The read file must exist (currently in the current directory)2 #buff3 =fil1.read () #一次性读完3Buff4 =fil1.read (100)#Read 100 bytes4 #print buff3+ "," +buff4 #这里如果先read () and then read (100) The Buff4 is empty, because read () has finished reading the entire file, just like the pointer in C has reached the end. You can print the current position of the pointer through the Fil1.tell () function5 PrintBuff46Fil1.close ()#Close File
Third, write the document
You can create a file object that can be written by opening the file by means of ' w '. If the file exists then empty the file and write it, if the file does not exist then create.
Example:
1 fil = open ( " yyc.txt , " w ", -1) # write If the file exists, empty the file and then write, and if the file does not exist, create the 2 fil.write ( 123456hello " 3 fil.flush () # It's best to refresh the 4 fil.close () # close file
Iv. read and write files r+, w+
' r ' mode open file created object can only read the contents of the file with the Read function, the ' W ' mode open file can only be written in the content. What if you want to write content and read content before a file is closed? This is what the ' r+ ' and ' w+ ' are capable of. However, it is recommended that you either read or write when a file is turned off, as this will cause strange problems, and you can read the following example carefully:
' w+ ' mode Example 1: If a write operation is performed between the open file and the closed file, then the last read is empty.
1 #write first and then read2FIL5 = open ('Yyc.txt',"w+", 0)#both written and read if the file exists, empty the file first, and if the file does not exist, create3Fil5.write ('b')#Write First4Fil5.flush ()#It's best to refresh after writing .5BUFF2 =fil5.read ()#because the pointer points to the back of the write, the content is empty6 Print 'w+ mode First write after read:'+buff2#the content that is read is not covered, and this is empty because the pointer points to the back of the write, the end of the document7Fil5.close ()
Example of ' w+ ' mode 2: If a read operation is performed before the file is opened and the file is closed, the read content is also empty.
1 #Read and write first2FIL5 = open ('Yyc.txt',"w+", 0)#both written and read if the file exists, empty the file first, and if the file does not exist, create3BUFF2 =fil5.read ()#The content is empty because the file is emptied4 Print 'w+ mode First read and write:'+buff2#the content that is read is not covered, and it is not written, so it is empty.5Fil5.write ('123456')#6Fil5.flush ()#It's best to refresh after writing .7Fil5.close ()
As can be seen from the above two examples, in the ' w+ ' mode to open the file and read and write operations, whether it is written after the first read or read after the write, read the content is empty , but the reason is different oh (the code comment is almost clear).
' r+ ' mode Example 1: If a write operation is performed between the open file and the closed file and then read, the content read is not overwritten after the write
1 #write first and then read2FIL3 = open ('Yyc.txt',"r+",-1)#overwrite content both read and write from the beginning of the file3Fil3.write ('66666')4Fil3.flush ()#It's best to refresh after writing .5BUFF2 = Fil3.read ()#the read operation must be refreshed before6 Print 'r+ mode First write after read:'+buff2#the content that is read is not overwritten because the pointer points to the post-write position after the file is written, that is, the end of the document7Fil3.close ()
Example of ' r+ ' mode 2: Run an error if you read and then write before opening the file and closing the file.
1 #Read and write first (error)2 #fil3 = open (' Yyc.txt ', "r+", -1) #既读又写 overwrite content from file start location3 #buff2 = Fil3.read () #先读4 #print ' r+ mode read and write: ' +buff2 #输出的内容是覆盖后的内容, that is to say, he executed the write? It's weird, and it's an error5 ## Fil3.flush () #加不加效果一样6 #fil3.write (' 66666 ') #后写7 #Fil3.flush () #写完最好刷新一下8 #fil3.close ()
V. Append write file a,a+
If you want to keep the existing content of the file, just add the new content to the end of the original file or add a file content without affecting the original content, you can use a or a + mode.
' A + ' mode Example 1: If a write operation is performed before the file is opened and the file is closed, then the last read is empty.
1 #write first and then read2FIL6 = open ('Yyc.txt','A +', 0)#Append If the file exists and create if the file does not exist3Fil6.write ('1234')#4Fil6.flush ()#It's best to refresh, to see if the file was successfully written.5 Print 'A + mode first write and then read:'+fil6.read ()#But the read is empty because the pointer points to the back of the write content6Fil6.close ()#Close File
' A + ' mode Example 2: If a read operation is performed before the file is opened and the file is closed, the content read is also empty.
1 #Read and write first2FIL6 = open ('Yyc.txt','A +', 0)#Append If the file exists and create if the file does not exist3 Print 'A + mode reads and writes first:'+fil6.read ()#But the reading is empty.4Fil6.write ('1234')#5Fil6.flush ()#It's best to refresh, to see if the file was successfully written.6Fil6.close ()#Close File
There are 6 examples, in fact, R+,w+,a+ has a similar point, that is, the location of the pointer to the file, determines the content of reading and writing . As long as you understand the position of the pointer in the file, it is not difficult to understand the file read and write.