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()方法
求解
2016-05-12
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
2016-05-12
好的 多謝 然而用seek()還是無法將異常提交到except處理,直接報錯了