Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 File tell() Method

Python3 File tell() Method

Python3 File Methods Python3 File methods


Overview

tell() The method returns the current position in the file, i.e. the current position of the file pointer.

Syntax

The tell() method syntax is as follows:


fileObject.tell()

Parameters

Return Value

Returns the current position in the file.

Example

The following example shows how to use the tell() method:

The contents of the runoob.txt file are as follows:


1:www.runoob.com
2:www.runoob.com
3:www.runoob.com
4:www.runoob.com
5:www.runoob.com

Read the file's contents in a loop:

Example (Python 3.0+)

#!/usr/bin/python3

Opening a File

fo

=

open

(

"

runoob.txt

"

,

"

r+

"

)

print

(

"

File name:

"

,

fo

.

name

)

line

=

fo

.

readline

(

)

print

(

"

The data read is: %s

"

%

(

line

)

)

Get the current file position

pos

=

fo

.

tell

(

)

print

(

"

Current position: %d

"

%

(

pos

)

)

Closing a File

fo

.

close

(

)

The output of the above example is:


文件名为:  runoob.txt
读取的数据为: 1:www.runoob.com

当前位置: 17

Python3 File Methods Python3 File methods