Python-os-lseek

提供:Dev Guides
移動先:案内検索

Python os.lseek()メソッド

説明

Pythonメソッド* 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はファイルの終わりを意味します。

戻り値

このメソッドは値を返しません。

次の例は、lseek()メソッドの使用法を示しています。

#!/usr/bin/python

import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
os.write(fd, "This is test")

# 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)
str = os.read(fd, 100)
print "Read String is : ", str

# Close opened file
os.close( fd )

print "Closed the file successfully!!"

上記のプログラムを実行すると、次の結果が生成されます-

Read String is :  This is test
Closed the file successfully!!