File函数 中的 file.tell()函数

首页 / Python2入门教程 / File函数 中的 file.tell()函数

Python file tell()返回文件中文件读/写指针的当前位置。

file.tell() - 语法

fileObject.tell()

file.tell() - 返回值

此方法返回文件中文件读/写指针的当前位置。

链接:https://www.learnfk.comhttps://www.learnfk.com/python/file-tell.html

来源:LearnFk无涯教程网

file.tell() - 示例

以下示例显示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: Python is a great language.

Current Position: 28

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

设计模式之美 -〔王争〕

To B市场品牌实战课 -〔曹林〕

Linux内核技术实战课 -〔邵亚方〕

容量保障核心技术与实战 -〔吴骏龙〕

Redis源码剖析与实战 -〔蒋德钧〕

林外 · 专利写作第一课 -〔林外〕

大厂设计进阶实战课 -〔小乔〕

现代React Web开发实战 -〔宋一玮〕

超级访谈:对话玉伯 -〔玉伯〕

好记忆不如烂笔头。留下您的足迹吧 :)