python中seek是什么意思_python中的seek问题

本文解析了Python中`open()`函数配合`seek()`和`write()`操作在不同模式下,如何影响`readlines()`方法的读取结果。通过实例对比,解释了为什么第1段代码读取的是偏移位置后的字符,而第2、3段则不同,揭示了'w'、'w+'和'r'模式对文件状态的影响。
摘要由CSDN通过智能技术生成

下面三段代码中的值怎么不一样呢?(1)myfile=open('filename.txt','w+')myfile.write('Mynameisella')myfile.seek(10)printmyfile.readlines()myfile.close()#运行结果为['ella'](2...

下面三段代码中的值怎么不一样呢?

(1)

myfile=open('filename.txt','w+')

myfile.write('My name is ella')

myfile.seek(10)

print myfile.readlines()

myfile.close()

#运行结果为 [' ella']

(2)

myfile=open('filename.txt','w')

myfile.write('My name is elle')

myfile.seek(10)

myfile.close()

myfile=open('filename.txt','r')

print myfile.readlines()

myfile.close()

#运行结果为:['My name is ella']

(3)

myfile=open('filename.txt','w')

myfile.seek(10)

myfile.write('My name is elle')

myfile.close()

myfile=open('filename.txt','r')

print myfile.readlines()

myfile.close()

#运行结果为['\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00My name is ella']

Udonotloveme 您好,不好意思,因为刚学习,所以还是没怎么明白,为什么同样是seek(10),第(1)段代码读到的是第11位以后的字符,但第(2)和(3)读到的还是全部字符呢?对于(1)我open了文件,还没有写东西,就seek(N),这里就会相当于有N个空格?对于(2)我执行了seek(N),才执行的read,读到的怎么是全部的字符呢,之前的seek没起作用?怎么不是从第11位开始读?而对于(3)之前seek(N)后面读的时候,之前的seek又起了作用的。麻烦回复细点哈,万分感谢!

展开

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值