我正忙于开发标记文档的工具。我似乎无法解决我遇到的问题。请参阅下面的代码,这两个函数只是我代码的一小部分:
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个文件,如果我将文件添加到文件夹im扫描中,它将打印错误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)
答案 0 :(得分:1)
如果你想要更少的代码行,没有类,你也可以这样做:
reported_errors = []
def errorLogOnce(err):
if not err in reported_errors:
reported_errors.append(err)
errorLog(err)
然后调用errorLogOnce而不是调用errorLog
答案 1 :(得分:0)
所以你想要一个永远不会重复相同错误日志的记录器? 如果是这样,请定义一个类
class MyLogger(object):
def __init__(self):
self.reported_errors = []
def log(self,err):
if not err in self.reported_errors:
self.repoted_errors.add(err)
errorLog(err)
并调用此记录器而不是直接直接调用errorLog(e)
def runSearch():
logger = MyLogger()
....
tagDocs(fullFile,logger)
....
def tagDocs(fullFile,logger):
try:
...
except Exception, e:
logger.log(e)
pass