“Python3 file tell”的版本间差异

本页内容
(创建页面,内容为“{{DISPLAYTITLE:file.tell()}}10 = Python3 File tell() 方法 = Python3 File(文件) 方法 === 概述 === '''tell()''' 方法返回文件的当前位置,即文件指针当前位置。 === 语法 === tell() 方法语法如下: <sample title="" desc="" lang="python" hererun="1"> fileObject.tell() </sample> === 参数 === * '''无''' === 返回值 === 返回文件的当前位置。 == 以下…”)
 
Neo讨论 | 贡献
(文本替换 - 替换“example_end”为“”)
 
(没有差异)

2022年8月17日 (三) 21:14的最新版本

Python3 File tell() 方法

Python3 File(文件) 方法

概述

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

语法

tell() 方法语法如下:


示例

fileObject.tell()

参数

返回值

返回文件的当前位置。

==

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

文件 xiaobai.txt 的内容如下:


示例

1:www.xiaobai.wang
2:www.xiaobai.wang
3:www.xiaobai.wang
4:www.xiaobai.wang
5:www.xiaobai.wang

循环读取文件的内容:

实例(Python 3.0+)

示例

#!/usr/bin/python3

# 打开文件
fo = open("xiaobai.txt", "r+")
print ("文件名为: ", fo.name)

line = fo.readline()
print ("读取的数据为: %s" % (line))

# 获取当前文件位置
pos = fo.tell()
print ("当前位置: %d" % (pos))

# 关闭文件
fo.close()

以上实例输出结果为:


示例

文件名为:  xiaobai.txt
读取的数据为: 1:www.xiaobai.wang

当前位置: 17

Python3 File(文件) 方法

此页面最后编辑于2022年8月17日 (星期三) 21:14。