python的seek用法
fileObject.seek(offset[, whence])
offset – 開始的偏移量,也就是代表需要移動偏移的字節數
whence:可選,默認值為 0。給offset參數一個定義,表示要從哪個位置開始偏移;0代表從文件開頭開始算起,1代表從當前位置開始算起,2代表從文件末尾算起。
1.seek(0,0)//從開頭偏移0 的位置
f1= open(‘D:\pythontxt2.txt’,‘rb’)
line = f1.readline()
print(’**************************’)
print(line)
f1.seek(0, 0)
print(’########################’)
line = f1.readline()
print(line)
2.seek(x,1)//從當前位置偏移x位 x 可為正負零
f1= open(‘D:\pythontxt2.txt’,‘rb’)
line = f1.readline()
print(’**************************’)
print(line)
f1.seek(2, 1)
print(’########################’)
line = f1.readline()
print(line)
f1= open(‘D:\pythontxt2.txt’,‘rb’)
line = f1.readline()
print(‘’)
print(line)
f1.seek(-5, 1)
print(’########################’)
line = f1.readline()
print(line)
3.seek(x,2)//從末尾位置偏移x位 x 可為正負零,正向偏移返回空值不報錯
f1= open(‘D:\pythontxt2.txt’,‘rb’)
line = f1.readline()
print('’)
print(line)
f1.seek(-6, 2)
print(’########################’)
line = f1.readline()
print(line)
總結
以上是生活随笔為你收集整理的python的seek用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单井字棋设计
- 下一篇: Sublime Text3中文乱码解决方