日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python的seek函数

發布時間:2023/12/20 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python的seek函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自:?這里


Python中的seek函數作用是: 移動文件的讀取指針到指定位置。seek函數需要使用文件對象進行調用,無返回值。


seek函數有三種模式:


1) f.seek(p, 0) 或 f.seek(p)


將文件讀取指針移動到文件的第p個字節處,表示絕對位置。f.seek(0)移動到文件頭位置。


2) f.seek(p, 1)


在當前位置的基礎上,將文件讀取指針移動p個字節,表示相對位置。


3) f.seek(p, 2)


在文件尾的基礎上,將文件讀取指針移動p個字節,表示相對位置。f.seek(0, 2)移動到文件尾位置。


代碼:

# --coding: utf-8 --#導入argv,并解包 from sys import argv script, filename = argv#函數,輸出文件讀取指針的當前位置 def print_current_position(f):print "Current position: ", f.tell()#函數,輸出文件的當前行 def print_current_line(f):print "Content of line: ", f.readline()f = open(filename, 'r'); #打開文件,并輸出全部內容 print_current_position(f) print "Content of file: \n", f.read() print_current_position(f)f.seek(0, 0) #文件讀取指針回到起始位置 print_current_position(f) print_current_line(f) print_current_position(f)f.seek(3, 1) #文件讀取指針移動當前位置之后的3個字節 print_current_position(f) print_current_line(f) print_current_position(f)f.seek(0, 2) #文件讀取指針移動到文件尾部 print_current_position(f) print_current_line(f) print_current_position(f)f.close()


結果:






總結

以上是生活随笔為你收集整理的Python的seek函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。