为了账号安全,请及时绑定邮箱和手机立即绑定

try中seek语句异常无法提交到except处理

try中seek方法异常无法提交到except处理,而是直接报错

try:        
    with open('test.txt') as fl: 
        print "in with file:", fl.read()
        os.lseek(fl, -5, os.SEEK_SET)
except ValueError, e:
    print "catch ValueError:", e
    print "with:", fl.closed

in with file: 123476868736db


Traceback (most recent call last):

  File "test03.py", line 32, in <module>

    os.lseek(fl, -5, os.SEEK_SET)

TypeError: an integer is required

这个和老师的实例不同啊,os.lseek()和file.seek()方法效果差不多,遇到的问题一样,我只是做下测试换用了os.lseek()方法

求解




正在回答

2 回答

os.lseek(fd, pos, how) 其中的fd(File descriptor)是要Type Integer

你使用open回傳是Type io.TextIOWrapper,需使用os.open()才是回傳Type Integer

所以他回傳TypeError, 因為你的第一個參數的Type不對

簡單來說:

os.open搭os.lseek
open搭file.seek


1 回复 有任何疑惑可以回复我~

好的 多谢 然而用seek()还是无法将异常提交到except处理,直接报错了

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Python错误和异常
  • 参与学习       31651    人
  • 解答问题       32    个

让我们一起来学习Python对于错误和异常的处理方式

进入课程

try中seek语句异常无法提交到except处理

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号