Read-write files are the most common IO operations. Python has built-in functions for reading and writing files, and the usage is compatible with C.
Before reading and writing files, we must first understand that the ability to read and write files on disk is provided by the operating system, the modern operating system does not allow ordinary programs to operate the disk directly, so the read-write file is to request the operating system to open a file object (often referred to as a file descriptor), and then, Read the data from this file object (read the file) from the interface provided by the operating system, or write the data to the file object (write file).
File Operation Flow
1 Open the file, get the file handle and assign a value to a variable.
2 Manipulating files by handle
3 Closing files
Test file:
1 how he wanted to be a grass 2 how he wanted to be a grass, dyed green in the wilderness 3 how he wanted to be a flying goose, to turn over the clouds 4 Even a wildfire, even a Reich. 5 but also a carefree, and a happy 6 how he wanted to be a grass, dyed green in the wilderness 7 how he wanted to be a flying goose, to turn over the clouds 8 Even a wildfire, even a Reich. 9 but also a carefree, and a happy Ten wasted the years, hurt the feelings Why, why, is there such an arrangement?
Basic operations Read Documents
f= open (' Banished.txt ', encoding= "Utf-8") #通过open函数打开一个文件, returns the file handle print (F.read ()) #读取文件内容f. Close () #关闭文件
Read the above for the content, the read () function will be the contents of the file read out all at once, if read two times what effect?
f = open (' Banished.txt ', ' R ', encoding= "utf-8") Data1 = F.read () data2 = F.read () print (data1) print ('---------------%s '% DATA2) F.close ()
Read it again but did not print the content. It turns out that when we operate on files, there is a file pointer inside the file to locate the current position, and the initial position 1 is moved by byte content. The Read () method reads all the contents and moves the file pointer to the end, at which point the Read () method is used to find that no content can be read to return NULL.
Write a document
The ' W ' mode write file creates a file that is overwritten by the original contents of the file. Files opened with this mode cannot be read using the Read () method.
Banished1.txt Document Content
# Author:zhang He wants to be a grass, he wants to be a grass, dyed green that wilderness he would like to be only flying geese, into the sea of clouds even if it is wildfire burning, even Reich also fell a carefree, also fell a happy he wants to be a grass, dyed green that wilderness he would like to be a flying goose, The surging cloud of clouds even if it is wildfire burning, even Reich also fell a carefree, also fell a happy leisurely wasted years, hurt the feelings why, why, biased to have such arrangements?
f = open (' Banished1.txt ', ' W ', encoding= ' Utf-8 ') f.write (' Old journey to the set a ') F.close ()
After you perform a write operation
Old journey to the set a
Read and write offset file pointers
f = open (' Banished1.txt ', ' W ', encoding= ' Utf-8 ') f.write (' Old journey to the set a \ n ') #内容换行f. Write (' very classic soundtrack ') F.close ()
After you finish writing the content
The old journey to the set A is a classic soundtrack.
' A ' document append this mode does not overwrite the original file.
f = open (' Banished1.txt ', ' a ', encoding= ' Utf-8 ') f.write (' \ n Xu Jianqing teacher Works ') F.close ()
After you finish writing the content
Old journey to the set a classic soundtrack Xu Jianqing teacher Works
New content has been appended to the end.
Reference Document: File read-write---liao Xuefeng Python3
File pointers for Python file processing (iv)
Python file operations