0

我正在创建一个工具,可以概述百分之一的测试结果。此工具访问日志文件,检查通过和失败的判断。当它失败时,我需要回到日志的前几行来捕获失败的原因。linecache.getline 在我的工作区中工作(Python Run via eclipse)。但是在我创建了一个 Windows 安装程序(.exe 文件)并在我的计算机中安装了应用程序之后,linecache.getline 什么也没有返回。我需要在 setup.py 文件中添加什么来解决这个问题还是我的代码问题?

工具代码

precon:从wx.FileDialog,访问日志文件

    self.result_path = dlg.GetPath() 
    try:
         with open(self.result_path, 'r') as file:
         self.checkLog(self.result_path, file)

def checkLog(self, path, f):

        line_no = 1
        index = 0
        for line in f:
             n = re.search("FAIL", line, re.IGNORECASE) or re.search("PASS", line, re.IGNORECASE)
             if n:
               currentline = re.sub('\s+', ' ', line.rstrip())
               finalresult = currentline


                self.list_ctrl.InsertStringItem(index, finaltestname)
                self.list_ctrl.SetStringItem(index, 1, finalresult)

                 if currentline == "FAIL":

                    fail_line1 = linecache.getline(path, int(line_no - 3)) #Get reason of failure
                    fail_line2 = linecache.getline(path, int(line_no - 2)) #Get reason of failure                    
                    cause = fail_line1.strip() + " " + fail_line2.strip()
                    self.list_ctrl.SetStringItem(index, 2, cause)

                    index += 1
             line_no += 1
4

1 回答 1

0

通过执行此链接中的 get_line 函数解决了该问题: Python: linecache not working as expected?

于 2017-09-25T22:42:17.217 回答