小白教程
所有教程
关于
Search
172.70.126.102
172.70.126.102
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 file tell”的源代码
本页内容
上一节:
Python3_file_seek
下一节:
Python3_file_truncate
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:file.tell()}}[[Category:python3 file methods|10]] = Python3 File tell() 方法 = [[python3 file methods|Python3 File(文件) 方法]] === 概述 === '''tell()''' 方法返回文件的当前位置,即文件指针当前位置。 === 语法 === tell() 方法语法如下: <sample title="" desc="" lang="python" hererun="1"> fileObject.tell() </sample> === 参数 === * '''无''' === 返回值 === 返回文件的当前位置。 == 以下实例演示了 tell() 方法的使用: 文件 xiaobai.txt 的内容如下: <sample title="" desc="" lang="python" hererun="1"> 1:www.xiaobai.wang 2:www.xiaobai.wang 3:www.xiaobai.wang 4:www.xiaobai.wang 5:www.xiaobai.wang </sample> 循环读取文件的内容: == 实例(Python 3.0+) == <sample title="" desc="" lang="python" hererun="1"> #!/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() </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> 文件名为: xiaobai.txt 读取的数据为: 1:www.xiaobai.wang 当前位置: 17 </sample> [[python3 file methods|Python3 File(文件) 方法]]
返回至“
Python3 file tell
”。
上一节:
Python3_file_seek
下一节:
Python3_file_truncate