Python:跳过错误消息并继续在python循环中?

时间:2016-03-23 18:39:30

标签: python python-2.7 exception

当data1不在file_i.hdf5中时:

KeyError: "unable to open object (Symbol table: Can't open object)"

1 个答案:

答案 0 :(得分:1)

使用try:except(KeyError):来捕获异常,如此。

import h5py

for i in range(0,100):
    try:
         with h5py.File('file_%s.hdf5' % i) as f:
             my_data = f['data1'][:]
             print i
    except(KeyError):
        # more code goes here...