python - 总是打印多行错误

标签 python printing

我正忙于开发一个用于标记文档的工具。我似乎无法解决我遇到的问题。请参阅下面的代码,这些是我的代码的一小部分的两个函数:

def runSearch():
time.sleep(1)
#checkt of de lengte van de list keywords groter is dan 0, zo niet, moet je eerst keywords opgeven
if len(keywords) > 0:
    path_input = raw_input('Give path to check for documents(e.g. /Users/Frank/Desktop): ')
    errCounter = 0

    #hier word gekeken of de opgegeven directory bestaat
    if os.path.isdir(path_input):
        for root, dirs, files in os.walk(path_input):
            for file in files:
                fullFile = os.path.join(root, file)
                if os.path.splitext(fullFile)[1].lower() == ('.docx') or \
                                os.path.splitext(file)[1].lower() == ('.doc') or \
                                os.path.splitext(file)[1].lower() ==  ('.pptx') or \
                                os.path.splitext(file)[1].lower() == ('.txt') or \
                                os.path.splitext(file)[1].lower() == ('.xlsx') or \
                                os.path.splitext(file)[1].lower() == ('.xls') or \
                                os.path.splitext(file)[1].lower() == ('.odt') or \
                                os.path.splitext(file)[1].lower() == ('.rtf') or \
                                os.path.splitext(file)[1].lower() == ('.csv') or \
                                os.path.splitext(file)[1].lower() == ('.html') or \
                                os.path.splitext(file)[1].lower() == ('.htm') or \
                                os.path.splitext(file)[1].lower() == ('.pdf'):
                    tagDocs(fullFile)

        print ('\nSearch for keyword(s) completed.')
        time.sleep(1)
    else:
        print("\nPlease enter a valid path, for example: '/Users/Frank/Documents'")
else:
    print('\nNo keywords were defined, use menu option 2 first.')
raw_input("\nPress Enter to continue...")
menu()

def tagDocs(fullFile):
    try:
        content = textract.process(os.path.abspath(fullFile))
        # contentList.append(content)
        words = content.split()
        words = [words.lower() for words in words]

        tagCounter = 0

        for item in enumerate(words):
            for index, value in enumerate(keywords):
                if value in item:
                    tagDocuments.append(fullFile)
                    tagCounter += 1

        if tagCounter != 0:
            print ("\nIn the file '" + os.path.abspath(fullFile) + "' the tag(s)" +  " was found: " +
                   str(tagCounter) + " time(s).")

    except Exception, e:
        errorLog(e)
        pass

每次有错误传递给 errorLog() 时,它都会在 error.log 中打印该错误 4 次。我知道是什么原因造成的,因为它循环了4次,因为有4个文件,如果我在扫描的文件夹中添加一个文件,它会打印错误5次。所以我的问题是,如何让它只打印错误 1 ​​次?现在,它会为找到的每个文件打印错误 1 ​​次。

更新#2

def errorLogOnce(err):
    if not err in reported_errors:
        reported_errors.append(err)
        errorLog(err)

def errorLog(e):
    errCounter = 0
    errCounter += 1

    logging.basicConfig(filename='error.log',level=logging.DEBUG)
    logging.debug(e)

最佳答案

如果你想要更少的代码行,没有类,你也可以这样做:

reported_errors = []

def errorLogOnce(err):
  if not err in reported_errors:
    reported_errors.append(err)
    errorLog(err)        

然后调用errorLogOnce而不是调用errorLog

关于python - 总是打印多行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34826363/

相关文章:

python - 在 Python 3 中操作嵌套字典/列表树

python - 如何获取时间 17 :00:00 today or yesterday?

python - 如何使用 perl 将控制权传递给不同的终端选项卡?

php - 在 Javascript 文件中在屏幕上打印 html 输入属性和变量

css - 分页后在 ff 中工作正常但在 ie 中不工作

css - ASP.NET MVC - 打印未占用整个页面宽度

python - 为什么我被迫在 python 中使用 os.path.expanduser?

python - 无法安装 tensorflow 1.3.0

html - 在 html/css 中创建三折

wpf - 在打印FlowDocument时避免多列