Python os.lseek() 方法
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 "關閉文件成功!!"
執行以上程序輸出結果為:
關閉文件成功!!