Python File tell() 方法

Python 教程 · 2019-02-03 15:57:38

概述

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

循环读取文件的内容:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("codercto.txt", "rw+")
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 教程 文章: https://codercto.com/courses/l/8.html

查看所有标签

Game Engine Architecture, Second Edition

Game Engine Architecture, Second Edition

Jason Gregory / A K Peters/CRC Press / 2014-8-15 / USD 69.95

A 2010 CHOICE outstanding academic title, this updated book covers the theory and practice of game engine software development. It explains practical concepts and techniques used by real game studios,......一起来看看 《Game Engine Architecture, Second Edition》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具