读写指针的问题
In [1]: i = open('0427text.txt','w+') #0427text.txt 没有文件的时候会创建文件
In [2]: i.read() #读取0427text.txt
Out[2]: '' #内容为空
In [3]: i.tell() #查看读写指针位置
Out[3]: 0 #读写指针位置为0
In [4]: i.write('223456')
In [5]: i.tell()
Out[5]: 6 #写入内容后指针为6
In [6]: i.read()
Out[6]: ''
In [7]: i.flush()
In [8]: i.read()
Out[8]: '' #此时读出内容为''这是因为读出时从读写指针后开始读取
In [9]: i.seek(0) #把读写指针移动回最开始位置
In [10]: i.tell()
Out[10]: 0
In [11]: i.read()
Out[11]: '223456' #读取出内容
In [12]: i.tell()
Out[12]: 6 #每次读或写都会把读写指针进行偏移
In [13]: i.seek(0)
In [14]: i.write('abc')
In [15]: i.tell()
Out[15]: 3
In [16]: i.read()
Out[16]: '456'
In [17]: i.seek(0)
In [18]: i.read()
Out[18]: 'abc456' #读写指针在0时写内容会覆盖后面内容
In [19]: i.close() #所以在新打开一个文件时直接写入会覆盖后面内容
In [1]: i = open('0427text.txt','w+') #0427text.txt 没有文件的时候会创建文件
In [2]: i.read() #读取0427text.txt
Out[2]: '' #内容为空
In [3]: i.tell() #查看读写指针位置
Out[3]: 0 #读写指针位置为0
In [4]: i.write('223456')
In [5]: i.tell()
Out[5]: 6 #写入内容后指针为6
In [6]: i.read()
Out[6]: ''
In [7]: i.flush()
In [8]: i.read()
Out[8]: '' #此时读出内容为''这是因为读出时从读写指针后开始读取
In [9]: i.seek(0) #把读写指针移动回最开始位置
In [10]: i.tell()
Out[10]: 0
In [11]: i.read()
Out[11]: '223456' #读取出内容
In [12]: i.tell()
Out[12]: 6 #每次读或写都会把读写指针进行偏移
In [13]: i.seek(0)
In [14]: i.write('abc')
In [15]: i.tell()
Out[15]: 3
In [16]: i.read()
Out[16]: '456'
In [17]: i.seek(0)
In [18]: i.read()
Out[18]: 'abc456' #读写指针在0时写内容会覆盖后面内容
In [19]: i.close() #所以在新打开一个文件时直接写入会覆盖后面内容