如何跳过在我需要继续处理的一长串文件中给出 ReferenceError 的文件?

问题描述 投票:0回答:0

在 python 框架中,我试图在用户的工作目录中存储几个满足特定条件的文件;这些文件是由不同的用户随意创建和销毁的,因此当存储代码通过工作目录时,我收到一个空指针“ReferenceError”错误,例如当文件仍然存在但没有其直方图时的下面的错误属性(又名变成空指针):

Error in <TFile::TFile>: file ./UL_2022_EE/fits_data/muon/generalTracks/Z/Run2022_EE/tagIsoUp/NUM_HighPtID_DEN_TrackerMuons/NUM_HighPtID_DEN_TrackerMuons_abseta_4_pt_5.root does not exist
ReferenceError: attempt to access a null-pointer

(基本上在某些文件中,没有任何东西可以被我的存储程序访问)

我已经尝试了 try-except 代码的许多变体,甚至是已经演变成这样的 if-then 语句:

    def catchgeese():
        '''                                                                                                                                                         
        catchgeese() finds the appropriate .png file for the selected fit failure, using pulls or other mathematical parameters, such as the chi2probability distribution, or KS.                                                                                                            
        catchgeese() is used within the 'for' loop to ensure access to TTree 'tests', or any other `VALIDATION parameters` set                                     
        '''
        for inFile in baseDir:

            try:
                myfile = inFile.replace(".root",".png")
                if not os.path.exists(baseDir + '/geese/'): os.makedirs(baseDir + '/geese/')

            except ReferenceError: 
                print("Failed job encountered. Please run `./tnp_fitter.py [...] -j 16 --recover` in order to retrieve all jobs")
                continue

            target = (baseDir + '/geese/'
            goosechase = shutil.copy(myfile, target+mytitle()+".png")

            return goosechase

其中

inFile
只是选择文件的每个单独路径。

我还尝试在 try-except 语句上方放置不同的 for 循环,因为如果

continue
不存在,则出现
for
不在循环内的错误,但是 for 循环给出了类似的错误(ZeroDivisionError: float 除以零),同时没有正确地遍历列表。我认为的问题是
for i in baseDir
并没有真正起作用,因为 baseDir 中的每个
inFile
都是一个检索路径,而不是一个迭代列表(我在这个问题上从论坛尝试过这个:Use try and except to skip a文件)。但是,因为只有在满足特定数学标准时才会调用此特定函数,所以在用户的工作目录中,我不确定如何在此处正确地遍历列表。我试过使用
os.path.exists()
函数之类的东西无济于事。任何指导将不胜感激!

python try-except continue skip
© www.soinside.com 2019 - 2024. All rights reserved.