'gbk' codec can't decode byte 0xae in position 2: illegal multibyte sequence和io.UnsupportedOperation: can't do nonzero end-relative seeks错误。
1.io.UnsupportedOperation: can't do nonzero end-relative seeks:
file='a_dir/pi'
with open(file,'r') as f:
print(f.readline())
f.seek(2,1)
print(f.readline())
D:\python\python.exe F:/code/b.py
File "F:/code/b.py", line 1
file='a_dir/pi'
^
IndentationError: unexpected indent
进程已结束,退出代码 1
之所以出现这种错误,是因为 读取文本时,seek()方法只有在文件开头(即from_what默认是0时)进行偏移是被允许的。不允许在当前所在位置(from_what=1)和文件末尾(from_what=2)时进行偏移。
接下来展示正确方法:
file='a_dir/pi'
with open(file,'r') as f:
print(f.readline())
f.seek(2,0)
print(f.readline())
D:\python\python.exe F:/code/b.py
我爱我的祖国
爱我的祖国
进程已结束,