Python3 File tell() 方法

Python 3 教程 · 2019-02-07 06:12:22

概述

tell() 方法返回文件的当前位置,即文件指针当前位置。

语法

tell() 方法语法如下:

fileObject.tell()

参数

  • 无

返回值

返回文件的当前位置。

实例

以下实例演示了 tell() 方法的使用:

文件 codercto.txt 的内容如下:

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

循环读取文件的内容:

实例(Python 3.0+)

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

以上实例输出结果为:

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

当前位置: 17

点击查看所有 Python 3 教程 文章: https://www.codercto.com/courses/l/10.html

查看所有标签

Blog Design Solutions

Blog Design Solutions

Richard Rutter、Andy Budd、Simon Collison、Chris J Davis、Michael Heilemann、Phil Sherry、David Powers、John Oxton / friendsofED / 2006-2-16 / USD 39.99

Blogging has moved rapidly from being a craze to become a core feature of the Internetfrom individuals sharing their thoughts with the world via online diaries, through fans talking about their favori......一起来看看 《Blog Design Solutions》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

MD5 加密
MD5 加密

MD5 加密工具