Python-file-tell

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

Pythonファイルのtell()メソッド

説明

Pythonファイルメソッド* tell()*は、ファイル内のファイル読み取り/書き込みポインターの現在の位置を返します。

構文

以下は* tell()*メソッドの構文です-

fileObject.tell()

パラメーター

  • NA

戻り値

このメソッドは、ファイル内のファイル読み取り/書き込みポインターの現在の位置を返します。

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

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
#!/usr/bin/python

# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

line = fo.readline()
print "Read Line: %s" % (line)

# Get the current position of the file.
pos = fo.tell()
print "Current Position: %d" % (pos)

# Close opend file
fo.close()

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

Name of the file:  foo.txt
Read Line: This is 1st line

Current Position: 28