描述
方法lseek()設置文件描述符的當前位置fd到給定的位置pos, 修改為how。
用法
以下是語法lseek()方法 -
os.lseek(fd, pos, how)
參數
fd− 這是需要處理的文件描述符。
pos- 這是相對於給定參數如何在文件中的位置。你給 os.SEEK_SET 或 0 來設置相對於文件開頭的位置, os.SEEK_CUR 或 1 來設置它相對於當前位置; os.SEEK_END 或 2 以相對於文件末尾進行設置。
how− 這是文件的參考點 with-in。 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!!
相關用法
- Python 3 os.lstat()用法及代碼示例
- Python 3 os.lchown()用法及代碼示例
- Python 3 os.lchmod()用法及代碼示例
- Python 3 os.lchflags()用法及代碼示例
- Python 3 os.link()用法及代碼示例
- Python 3 os.listdir()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 os.rmdir()用法及代碼示例
- Python 3 os.fdopen()用法及代碼示例
- Python 3 os.fdatasync()用法及代碼示例
- Python 3 os.isatty()用法及代碼示例
- Python 3 os.rename()用法及代碼示例
- Python 3 os.walk()用法及代碼示例
- Python 3 os.renames()用法及代碼示例
- Python 3 os.makedirs()用法及代碼示例
- Python 3 os.utime()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.lseek() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。