Python os.lseek() 方法
概述
os.lseek() 方法用於設置檔描述符 fd 當前位置為 pos, how 方式修改。
在Unix,Windows中有效。
語法
lseek()方法語法格式如下:
os.lseek(fd, pos, how)
參數
fd -- 檔描述符。
pos -- 這是相對於給定的參數 how 在檔中的位置。。
how -- 檔內參考位置。SEEK_SET 或者 0 設置從檔開始的計算的pos; SEEK_CUR或者 1 則從當前位置計算; os.SEEK_END或者2則從檔尾部開始。
返回值
該方法沒有返回值。
實例
以下實例演示了 lseek() 方法的使用:
#!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys # 打開檔 fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # 寫入字串 os.write(fd, "This is test") # 所有 fsync() 方法 os.fsync(fd) # 從開始位置讀取字串 os.lseek(fd, 0, 0) str = os.read(fd, 100) print "Read String is : ", str # 關閉檔 os.close( fd ) print "關閉檔成功!!"
執行以上程式輸出結果為:
關閉檔成功!!