Home  >  Article  >  Backend Development  >  Python's File tell() method usage and function analysis (example)

Python's File tell() method usage and function analysis (example)

乌拉乌拉~
乌拉乌拉~Original
2018-08-16 17:36:273478browse

In today’s article, we will learn about the tell method in Python. Before entering the article, we must first know what pythontell() is, tell() Where can it be used in python programming and what role can it play. We can learn about these things in today’s article.

Overview

The tell() method returns the current position of the file, which is the current position of the file pointer.

Syntax

The tell() method syntax is as follows:

fileObject.tell(offset[, whence])

Return value

Returns the value of the file current location.

Example

The following example demonstrates the use of the tell() method:

The content of the file runoob.txt is as follows:

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

Loop to read the contents of the file:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开文件
fo = open("runoob.txt", "rw+")
print "文件名为: ", fo.name
line = fo.readline()
print "读取的数据为: %s" % (line)
# 获取当前文件位置
pos = fo.tell()
print "当前位置: %d" % (pos)
# 关闭文件
fo.close()

The output result of the above example is:

文件名为:  runoob.txt
读取的数据为: 1:www.runoob.com
当前位置: 17

In this article, we explain what tell() is If you don’t understand the method, you can give it a try. After all, hands-on practice is the best way to verify what you have learned. Finally, I hope this article can bring some help to you who are learning python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of Python's File tell() method usage and function analysis (example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn