Python3 File tell() Method
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
-
None
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+
"
)
(
"
File name:
"
,
fo
.
name
)
line
=
fo
.
readline
(
)
(
"
The data read is: %s
"
%
(
line
)
)
Get the current file position
pos
=
fo
.
tell
(
)
(
"
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