Python3-os-lseek
提供:Dev Guides
Python 3-os.lseek()メソッド
説明
メソッド* lseek()は、ファイル記述子 *fd の現在位置を、 how によって変更された指定位置 pos に設定します。
構文
以下は* lseek()*メソッドの構文です-
os.lseek(fd, pos, how)
パラメーター
- fd -これはファイル記述子であり、処理する必要があります。
- pos -これは、指定されたパラメータhowに関するファイル内の位置です。 os.SEEK_SETまたは0を指定すると、ファイルの先頭を基準とした位置が設定され、os.SEEK_CURまたは1を指定すると、現在の位置を基準にして設定されます。 os.SEEK_ENDまたは2を使用して、ファイルの終わりを基準に設定します。
- 方法-これはファイル内の参照ポイントです。 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!!