亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號