python - Always print multiple error lines -
i'm busy working on tool tagging documents. can't seem solve issue im having. see code below, these 2 functions small part of code:
def runsearch(): time.sleep(1) #checkt of de lengte van de list keywords groter dan 0, zo niet, moet je eerst keywords opgeven if len(keywords) > 0: path_input = raw_input('give path check documents(e.g. /users/frank/desktop): ') errcounter = 0 #hier word gekeken of de opgegeven directory bestaat if os.path.isdir(path_input): root, dirs, files in os.walk(path_input): 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 keyword(s) completed.') time.sleep(1) else: print("\nplease enter valid path, example: '/users/frank/documents'") else: print('\nno keywords defined, use menu option 2 first.') raw_input("\npress enter continue...") menu() def tagdocs(fullfile): try: content = textract.process(os.path.abspath(fullfile)) # contentlist.append(content) words = content.split() words = [words.lower() words in words] tagcounter = 0 item in enumerate(words): index, value in enumerate(keywords): if value in item: tagdocuments.append(fullfile) tagcounter += 1 if tagcounter != 0: print ("\nin file '" + os.path.abspath(fullfile) + "' tag(s)" + " found: " + str(tagcounter) + " time(s).") except exception, e: errorlog(e) pass
every time there's error passed errorlog() print error 4 times in error.log. know what's causing this, it's because loops 4 times, because there 4 files, if add file folder im scanning, print error 5 times. question, how can make print error 1 time? prints error 1 time every file finds.
update #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)
if want less lines of code, without classes, can this:
reported_errors = [] def errorlogonce(err): if not err in reported_errors: reported_errors.append(err) errorlog(err)
and call errorlogonce instead of calling errorlog
Comments
Post a Comment