#!/usr/bin/python

try:
    f = open("/home/masi/r.raw", "r+")
    aBuf = f.seek(4)
except:
    print "Error at position : ", position

events = []
for i in range(100):
    aBuf = f.seek(4);

    try:
        if aBuf[:4] == b'\xFA\xFA\xFA\xFA':
            print("E")

    except:
    #   print "\0"

f.close()


当我注释掉除外的打印时,出现错误

    f.close()
    ^
IndentationError: expected an indented block


我认为我需要尝试,因为if子句经常是错误的。

为什么会得到这种意外的意外?

最佳答案

except需要一个或多个语句。

您可以这样写:

try:
    if aBuf[:4] == b'\xFA\xFA\xFA\xFA':
        print("E")
except:
    pass


警告
如评论中所述,使用except: pass不是一个好习惯。
 请参见Why is “except: pass” a bad programming practice?

关于python - 为什么这是Python中意外的缩进?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31318458/

10-09 03:36