seek()异常

f=open(‘aabb.py’,‘w’)
f.write(b’0123456789abcdef’)
Traceback (most recent call last):
File “<pyshell#1>”, line 1, in
f.write(b’0123456789abcdef’)
TypeError: write() argument must be str, not bytes

x=f.write(b’0123456789abcdef’)
Traceback (most recent call last):
File “<pyshell#2>”, line 1, in
x=f.write(b’0123456789abcdef’)
TypeError: write() argument must be str, not bytes

x=(b’0123456789abcdef’)
s=str(x)
f.write(s)
19

f.seek(5)
5

f.seek(1)
1

f.seek(-3,2) #此处出现错误是因为在文本文件中,没有使用b模式选项打开的文件,只允许从文件头开始计算相对位置,从文件尾计算时就会引发异常。
Traceback (most recent call last):
File “<pyshell#8>”, line 1, in
f.seek(-3,2)
io.UnsupportedOperation: can’t do nonzero end-relative seeks
以上出现的全部错误改正得

f=open(‘aabb.py’,‘rb+’) #以二进制格式打开一个文件用于读写
f.write(b’0123456789abcdef’)
16

f.seek(-3,2)
16

posted @ 2019-08-03 17:43  yunduoyun  阅读(78)  评论(0编辑  收藏  举报