Python中tell()方法的使用详解

yipeiwu_com6年前Python基础

 tell()方法返回的文件内的文件读/写指针的当前位置。
语法

以下是tell()方法的语法:

fileObject.tell()

参数

  •     NA

返回值

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

下面的例子显示了tell()方法的使用。

#!/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: This is 1st line

Current Position: 18


相关文章

python之列表推导式的用法

1. 定义 用一行构建代码 例题 # 构建一个1-100的列表 l1 = [i for i in range(1,101)] print(l1) # 输出结果 [1,2,3,4...

python 打印直角三角形,等边三角形,菱形,正方形的代码

三角形 等腰直角三角形1 2.7 #coding:utf-8 rows = int(raw_input('输入列数: ')) i = j = k = 1 #声明变量,i用于控制外层循...

Python的Django框架中的URL配置与松耦合

现在是好时机来指出Django和URL配置背后的哲学: 松耦合 原则。 简单的说,松耦合是一个 重要的保证互换性的软件开发方法。 Django的URL配置就是一个很好的例子。 在Djan...

python实现的AES双向对称加密解密与用法分析

本文实例讲述了python实现的AES双向对称加密解密与用法。分享给大家供大家参考,具体如下: 高级加密标准(Advanced Encryption Standard,AES),在密码学...

python3实现斐波那契数列(4种方法)

基础版(list方法) # 比较占内存 w = int(input("输入一个数字还你一个斐波那契数列:")) list_res = [] def list_n(n): if...