lseek()方法設置檔描述符fd的當前位置為給定的位置pos,由 how 修改
語法
下麵是 lseek()方法的語法:
os.lseek(fd, pos, how)
參數
-
fd -- 這是需要進行處理的檔描述符
-
pos -- 這是相對於給定的參數 how 在該檔中的位置。 給定 os.SEEK_SET 或 0 來設置檔相對的位置為開始,os.SEEK_CUR 或 1 將其設置為相對於當前位置; os.SEEK_END或2設置它相對於檔的末尾。
-
how -- 這是在檔內的參考點。 os.SEEK_SET 或0 意味著檔的開頭,os.SEEK_CUR或1意味著的當前位置,以及 os.SEEK_END 或 2 表示檔的結束。
定義常量pos
- os.SEEK_SET - 0
- os.SEEK_CUR - 1
- os.SEEK_END - 2
返回值
此方法不返回任何值。
示例
下麵的示例說明 lseek()方法的使用。
#!/usr/bin/python3 import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Write one string line="This is test" b=line.encode() os.write(fd, b) # Now you can use fsync() method. # Infact here you would not be able to see its effect. os.fsync(fd) # Now read this file from the beginning os.lseek(fd, 0, 0) line = os.read(fd, 100) print ("Read String is : ", line.decode()) # Close opened file os.close( fd ) print "Closed the file successfully!!"
當我們運行上面的程式,它會產生以下結果:
Read String is : This is test Closed the file successfully!!
上一篇:
Python3檔方法
下一篇:
Python3 os檔目錄的方法