“ try ..... except”问题引发了ROOT文件的语法错误

时间:2019-05-16 14:17:00

标签: python

我正在尝试在目录中找到损坏的根文件。基本上,尝试以下操作时,我的代码将抛出SyntaxError:无效语法

import ROOT as root
import sys
import glob


dir=sys.argv[1]
txtfiles = []
for file in glob.glob(dir+"/*.root"):
    f = root.TFile(file,'r')
    try :
        myTree = f.Get("AC1B")

        except IOError:
            print 'this is not good',f
            continue
        else :
            print 'the filename is ',file, myTree.GetEntries()

我也尝试过,除了:但这也不起作用。

1 个答案:

答案 0 :(得分:5)

您的except语句必须与try处于相同的缩进级别。它当前嵌套在try中:

import ROOT as root
import sys
import glob


dir = sys.argv[1]
txtfiles = []
for file in glob.glob(dir + "/*.root"):
    f = root.TFile(file,'r')
    try:
        myTree = f.Get("AC1B")

    except IOError:
        print 'this is not good',f
        continue
    else:
        print 'the filename is ',file, myTree.GetEntries()