In the previous article, we briefly described how to open a file, as well as read, write, and append actions, and Click here to view.
In this article, we continue to introduce another way to open a file and several simultaneous read and write modes.
First, open the file method : With
When you use file () or open (), you must finally use the close () method to close the file, in order to avoid forgetting to use, but also use the method with the open file, code such as:
with open (' filename ', ' mode ') as f: ...
Where f is the file handle, and the file is opened in the same way as f=open (' filename ', ' mode ')
This method can also open multiple files at once, such as:
with open (' filename 1 ', ' mode ') as f1,open (' filename 2 ', ' mode ') as F2 ...
When too many files lead to a long line of code, it is recommended that you use the following notation:
with open (' filename 1 ', ' mode ') as F1, open (' filename 2 ', ' mode ') as F2: ...
Let's use closed () to determine if the file is closed
The #使用open () method opens without joining close () >>> f=open (' Info.log ', ' R ') >>> print f.closed () false# Open with the open () method, Join Close () >>> f=open (' Info.log ', ' R ') >>> f.close () >>> print f.closed () true# using with open > >> with open (' Info.log ', ' R '):>>> print f.closed () #注意print一定要与with处于同一个结构层次下True
Second, several simultaneous reading and writing modes
In Python, in addition to the normal open mode (R, W, a), there are several simultaneous read and write modes, including:
r+: Read and write, readable and writable, if the file does not exist, error
w+: Read-write, readable and writable, create new files
A +: Append and read, readable can be appended, if the file does not exist, create
Third, the pointer
To facilitate understanding of both read and write modes, first introduce pointers
There is a file Info.log file content as follows:
① gets the pointer to the file handle, tell ()
Example:
>>> f=file (' Info.log ', ' R ') >>> print F.tell () #获取初始时的指针 >>> print f.readline () # Read the first line of the file >>> print F.tell () #获取读完第一行后的指针 >>> f.close () 0 #第一次指针的位置aaaa5 #第二次指针的位置
② setting the pointer to the file handle seek ()
Example:
>>> f=file (' Info.log ', ' R ') >>> print F.tell () >>> print f.readline () >>> F.seek (3) #设置指针位置 >>> Print F.tell () >>> f.close ()
Iv. Reading and writing:r+
Code:
#使用r + Open file without read () >>> f=open (' Info.log ', ' r+ ') >>> F.seek (5) #指针指向第二行 >>> Print F.tell () >>> f.write (' 1111 ') >>> print F.tell () >>> F.close (59)
File results:
Note: when using r+ to open a file, if read () is used, the new content is always at the end of the file, regardless of where the pointer is, with the write () method, unless the pointer is re-specified after read () . As follows:
>>> f=open (' Info.log ', ' r+ ') >>> f.readline () #读取文件第一行内容 >>> print F.tell () >> > f.write (' 1111 ') >>> print F.tell () >>> f.close () 525
File results:
Five, write read:w+
>>> f=open (' Info.log ', ' w+ ') >>> print f.read () >>> f.write (' 1111\n ') >>> F.write (' 2222 ') >>> print F.tell () >>> pring f.read () >>> f.seek (0) >>> print f.read ( ) >>> f.close () #执行结果 #由于是w + mode, so the original file content is overwritten, the content is empty, and the print f.read () result is empty 8 #两次write () to see the pointer position #此时指针处于文件末尾, the result of Pring F.read () is an empty 11112222 #重新把指针指向文件开头后, and then Print F.read (), with results
Vi. Additional Reading: a+
>>> f=open (' Info.log ', ' A + ') >>> print F.tell () >>> print f.read () >>> f.write (' 1111 ') >>> print F.tell () >>> print f.read () >>> f.seek (0) >>> print f.read () >>> F.close () #执行结果21 #应为是a + mode is on, the pointer points to the end of the file #指针在文件结果, so the print f.read () content is empty #写入内容后, looking at the pointer position #写入内容后, The pointer is still at the end, so print f.read () content or empty aaaa #重新定位指针后, Print f.read () out of the content bbbbcccccdddd
Python Learning Notes-file Basic operations (II)