python小白必看的文件指针用法

引出问题

或许有的小白不知道这个文件指针就会出现以下问题:
案例一:
代码含义:写入三句话,并调用两次read()函数读取两次文本

file = open("C:\\try.txt",'w',encoding="utf-8")
file.write("I'm the first row.\n")
file.write("I'm the second row.\n")
file.write("I'm the third row.\n")
file.close()

file=open("C:\\try.txt",'r',encoding="utf-8")
print("第一眼")
print(file.read())
print("第二眼")
print(file.read())
file.close()

print("程序执行完毕")

'''
运行结果:
第一眼
I'm the first row.
I'm the second row.
I'm the third row.

第二眼

程序执行完毕

'''

案例二:
代码含义,统计文本行数以及单词有多少个及单词出现的次数

with open("C:\\try.txt",'r+',encoding="utf-8") as f:
    f.write("I'm the first row.\n")
    f.write("I'm the second row.\n")
    f.write("I'm the third row.\n")

    line=f.read().count("\n")
    print("行数是:", line)

    print("单词的个数是:", line + f.read().count(" "))

    word=f.read().replace("\n", " ").replace(".","").split(" ")
    word.pop()
    dic={}
    for i in word:
        dic[i]=[word.count(i)]
    print(dic)
'''
运行结果:

行数是: 0
单词的个数是: 0
{}
'''

提出问题:咦!为什么都没有成功呢?其实代码是对的,就是忽略了文件的指针,这两个案例都是同一个问题,那到底是什么因素导致我们无法二次读取呢?
这是因为python读取文本采用了文本指针,指针在哪就在哪写,指针在哪,你就只能读取指针后面的东西。
举个例子
注意:这个文件中写入的还是案例一,写入进去的三句话。

file=open("C:\\try.txt",'r',encoding="utf-8")
print(file.read(1))
print(file.read(2))
print(file.read(3))

'''
运行结果:

I
'm
 th
'''

结论:可以看到第一个read()里面的参数是1,表示读取指针后面一个元素,读取完,指针后移动一位,第二个read()里面的参数是2,表示读取指针后面的两个元素,读取完,指针后移动两位等等,以此类推。这就为什么能解释为什么案例一与案例二的问题了,读取完或者写完,指针都在文本的结尾处了,无论你做什么操作,都无法查看。

解决问题

那么我们知道原因的所在了,引出今天的正题!!!
seek()函数设置光标的位置
tell()函数获取文件的指针位置
使用seek()设置指针的位置,以便重新读取
seek(x,y)里面有两个参数
x代表从文件开头的第几个字节位置开始读取
y有三个选项,主要是以什么为参考点,来进行指针移动的。
(1)0
表示绝对位置(文件的开头位置),如果不选第二个参数,默认为0。
(2)1
表示相对位置(光标的当前位置)。
(3)2
表示光标在文件的末尾,可以从文件的末尾,移动几个位置后开始读。
注意:x都可以是负数,要不在y为2的时候用正数当然会报错,所以得用负数。
案例一:使用seek()与tell()函数后的效果

file = open("C:\\try.txt", 'w', encoding="utf-8")
file.write("I'm the first row.\n")
file.write("I'm the second row.\n")
file.write("I'm the third row.\n")
file.close()

file = open("C:\\try.txt", 'r', encoding="utf-8")
print("第一眼")
print(file.read())
print("指针的位置:", file.tell())
file.seek(0)
print("指针的位置:", file.tell())

print("第二眼")
print(file.read())
file.close()

'''
运行结果:

第一眼
I'm the first row.
I'm the second row.
I'm the third row.

指针的位置: 61
指针的位置: 0
第二眼
I'm the first row.
I'm the second row.
I'm the third row.
'''

结论:二次读取成功,说明我们之前的分析正确。
那么我们也试试把seek()函数加入到第二个案例里吧!
案例二:使用seek()函数后的效果。

with open("C:\\try.txt", 'r+', encoding="utf-8") as f:
    f.write("I'm the first row.\n")
    f.write("I'm the second row.\n")
    f.write("I'm the third row.\n")
    f.seek(0)
    line = f.read().count("\n")
    print("行数是:", line)
    f.seek(0)
    print("单词的个数是:", line + f.read().count(" "))
    f.seek(0)
    word = f.read().replace("\n", " ").replace(".", "").split(" ")
    word.pop()
    dic = {}
    for i in word:
        dic[i] = [word.count(i)]
    print(dic)

'''
运行结果:

行数是: 3
单词的个数是: 12
{"I'm": [3], 'the': [3], 'first': [1], 'row': [3], 'second': [1], 'third': [1]}
'''

来一波,推送吧!
群号:781121386
群名:人生苦短,我学编程
欢迎大家加入我们,一起交流技术!!!

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值