菜鸟教程上有一段关于file.seek()方法的讲解,先简短描述一下seek()方法:html
文件runoob.txt的内容以下:python
1:www.runoob.com 2:www.runoob.com 3:www.runoob.com 4:www.runoob.com 5:www.runoob.com
循环读取文件的内容:windows
#打开文件 fo = open("runoob.txt", "r+") print("文件名为: ", fo.name) line = fo.readline() print("读取的数据为: %s" % line) #从新设置文件读取指针到开头 fo.seek(2, 1) line = fo.readline() print("读取的数据为: %s" % line) #关闭文件 fo.close()
运行一下发现报错了:编码
D:\Program\python34\python.exe D:/python_workshop/python6/study_file.py Traceback (most recent call last): 文件名为: runoob.txt File "D:/python_workshop/python6/study_file.py", line 158, in <module> 读取的数据为: 1:www.runoob.com fo.seek(2, 1) io.UnsupportedOperation: can't do nonzero cur-relative seeks Process finished with exit code 1
这要要从文本文件和二进制文件提及了。spa
从广义(物理意义)上说,二进制文件包括了文本文件,由于计算机最终存储的是二进制文件,因此物理意义上二者就是一回事,但从狭义(逻辑)上来讲,二者存储的方式又有所不一样。操作系统
文本文件又叫ASCII文件,这种文件在磁盘上存放时,每一个字符(8个bit位)对应一个字节,用于存放对应的ASCII码,例如:.net
ASCII 码 00110101 00110110 00110111 00111000指针
| | | |code
5 6 7 8
htm
共占用了4个字节,ASCII文件可在屏幕上按字符显示。
二进制文件,是按二进制编码存放文件的,如5678的存储方式为:00010110 00101110,只占用2个字节,二进制文件虽然也可在屏幕上显示,但其内容没法读懂。C系统在处理这些文件时,并不区分类型,都当作是字符流,按字节进行处理。 输入输出字符流的开始和结束只由程序控制而不受物理符号(如回车符)的控制。 ,所以也把这种文件称做"流式文件"。
在读取文档时,python认为0x1A(26)是文档结束符(EOF),因此有时使用"r"读取二进制文件时,可能会出现读取不全的现象,如:
二进制文件中存在以下从低位向高位排列的数据:7F 32 1A 2F 3D 2C 12 2E 76
若是使用'r'进行读取,则读到第三个字节,即认为文件结束
若是使用'rb'按照二进制位进行读取的,不会将读取的字节转换成字符,从而避免了上面的错误
在写入文档时,写入'\n',windows操做系统会隐式的将'\n'转换为"\r\n",再写入到文件中;读的时候,会把“\r\n”隐式转化为'\n',再读到变量中,而二进制文件是非解释性的,一次处理一个字符,而且不转换字符。
To change the file object’s position, use f.seek(offset, from_what)
. The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.
意思是,为了改变文件对象的位置,可使用f.seek(offset, from_what)方法。最终位置等于参考点位置加上偏移量,参考点是由from_what参数决定的。from_what有三个值,0表明文件的开始位置,1表明使用当前的位置,2表明文件的末尾位置。from_what参数省略时,默认是0。
python官方文档也给了一个实例:
>>> f = open('workfile', 'rb+') #咱们能够看出,文件workfile是以二进制格式进行读写的 >>> f.write(b'0123456789abcdef') 16 >>> f.seek(5) # Go to the 6th byte in the file 5 >>> f.read(1) b'5' >>> f.seek(-3, 2) # Go to the 3rd byte before the end 13 >>> f.read(1) b'd'
In text files (those opened without a b
in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2)
)
在文本文件中,只有在文件开头(即from_what默认是0时)进行偏移是被容许的。不容许在当前所在位置(from_what=1)和文件末尾(from_what=2)时进行偏移。
如今回头看看咱们的第一个代码和报错信息,发现咱们的读写模式用了"r+",咱们的文件是文本文件,在使用fo.seek()时,将from_what的值设置为1,因此报错了。
#打开文件 fo = open("runoob.txt", "r+") print("文件名为: ", fo.name) line = fo.readline() print("读取的数据为: %s" % line) #从新设置文件读取指针带开头 fo.seek(2, 1) line = fo.readline() print("读取的数据为: %s" % line) #关闭文件 fo.close()
D:\Program\python34\python.exe D:/python_workshop/python6/study_file.py Traceback (most recent call last): 文件名为: runoob.txt File "D:/python_workshop/python6/study_file.py", line 158, in <module> 读取的数据为: 1:www.runoob.com fo.seek(2, 1) io.UnsupportedOperation: can't do nonzero cur-relative seeks Process finished with exit code 1
若是咱们把读写模式改成"rb+",以二进制的方式进行读写,那么是否是就能够了,确实能够,但不容忽视的一个小问题是,二进制文件不会对windows下的换行(\r\n, 0x0D 0x0A)进行转化的,咱们看到的结果将是:
文件名为: runoob.txt 读取的数据为: b'1:www.runoob.com\r\n' 读取的数据为: b'www.runoob.com\r\n'
https://blog.csdn.net/timberwolf_2012/article/details/28499615
https://www.zhihu.com/question/19971994
https://docs.python.org/3.4/tutorial/inputoutput.html
http://bbs.fishc.com/thread-60449-1-1.html
https://www.cnblogs.com/kingleft/p/5142469.html
https://www.cnblogs.com/xisheng/p/7636736.html
https://bbs.csdn.net/wap/topics/350127738
https://www.cnblogs.com/pengwangguoyh/articles/3223072.html