Python Forum
List of error codes to find (and count) in all files in a directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of error codes to find (and count) in all files in a directory
#1
Greetings!
I have a list of error codes and I'm trying to find/count each error in each file and print them like this
"error - 1002x01 - 11"
"error - 1001x01 - 8"
and so on.
I wrote a code to do this but having a problem printing "error code - number of errors"

import os

list = ['100x02','1001x03']   # list of errors#
path = 'C:\\02'
for root,dirs, files in os.walk(path):
    for ef in  files :
        cur_ef = os.path.join(path,ef)
        if os.path.isfile(cur_ef):
        
            with open (cur_ef,'r') as fto_read  :
                count = 0                
                for e_ln in fto_read :           # reading each line from a file #
                    e_ln=e_ln.rstrip()
                    #print (e_ln)
                    for e_el in list :           # reading an element from the list #
                        if e_el in e_ln :
                            e_el=e_el.rstrip()
                            #print ("FOUND LINE --->> ",e_ln)
                            count+=1
                            print ("Error Name   ",e_el)
                print ("Number of Errors ---- >> ",count)
Reply
#2
What is the problem you're having? Does it work? Does it output something unexpected? Can you show the output?

On line3 you create a variable called list. That's usually a bad idea because it shadows the built-in list.

What is the purpose of line 17? e_el came from your list of errors. There should be no reason to strip them every time through the loop.
Reply
#3
Printing is a problem.
Here is what the script is printing:
Number of Errors ---- >>  0
Number of Errors ---- >>  0
Number of Errors ---- >>  0
Number of Errors ---- >>  0
Error Name    100x02
Error Name    100x02
Number of Errors ---- >>  2
Error Name    1001x03
Error Name    1001x03
Number of Errors ---- >>  2
Number of Errors ---- >>  0
Number of Errors ---- >>  0
Number of Errors ---- >>  0
Number of Errors ---- >>  0
Number of Errors ---- >>  0
Number of Errors ---- >>  0
>>>
I want to print it like this:
100x02 - 2
1001x03 - 2
e_el - is an element of the list (error codes).
I'm trying to search each element of the list in each file.
If found, count it and print out a total number of elements(error codes) and the error code.

The script does not produce errors.
I'm still struggling with God damn Python spacing!
I hope this helps to understand what I'm trying to do.
Thank you.
Reply
#4
You've got the count and the error name. So you can just print them out at the end if the count is non-zero.

From
                for e_ln in fto_read :           # reading each line from a file #
                    e_ln=e_ln.rstrip()
                    #print (e_ln)
                    for e_el in list :           # reading an element from the list #
                        if e_el in e_ln :
                            e_el=e_el.rstrip()
                            #print ("FOUND LINE --->> ",e_ln)
                            count+=1
                            print ("Error Name   ",e_el)
                print ("Number of Errors ---- >> ",count)
to
                for e_ln in fto_read :           # reading each line from a file #
                    e_ln=e_ln.rstrip()
                    #print (e_ln)
                    for e_el in list :           # reading an element from the list #
                        if e_el in e_ln :
                            e_el=e_el.rstrip()
                            #print ("FOUND LINE --->> ",e_ln)
                            count+=1
                    if count > 0:
                        print(f"{e_el} - {count}")
tester_V likes this post
Reply
#5
I tried your snippet.
Here is what is it printing out:
1001x03 - 1
1001x03 - 1
1001x03 - 1
1001x03 - 1
1001x03 - 1
1001x03 - 2
1001x03 - 1
1001x03 - 1
1001x03 - 1
1001x03 - 1
1001x03 - 2
1001x03 - 2
I do not know why I'm having such a hard time with Python "print" placing.
Anyway...
I made 12 test files in a directory.
One of the files has 2(two) '100x02' error codes,
Second file has 2(two) '1001x03' error codes.
10 files do not have any 'error codes'
I need to print out something like this:

100x02 - 2
1001x03 - 2


Two of each error found and I'll redirect (those two lines) the output to a different file later (will add more code)

Thank you!
Reply
#6
Change the print line from
print(f"{e_el} - {count}")
to
print(f"{cur_ef}: {e_el} - {count}")
Are they all coming from different files?
Reply
#7
I made 12 test files in a directory to the the script.
One of the files has 2(two) '100x02' error codes,
Second file has 2(two) '1001x03' error codes.
10 files do not have any 'error codes'
I'd like to print total number of each error.
script suppose to print only:
100x02 - 2
1001x03 - 2
Where:
1. '100x02' is the error code and '2' is the total number of errors
2. '1001x03 ' is the error code and '2' is the total number of errors

I replaced
print(f"{e_el} - {count}")
with
print(f"{cur_ef}: {e_el} - {count}")
And now the script prints "File-error code - number of errors'
I just want total number of each error found in the files (al files)

Sorry for the confusion!
And I appreciate your help.
Reply
#8
I finallyI figured out where to move the:
if count > 0:
    print(f"{cur_ef}: {e_el} - {count}")
And now it almost does what I need, it prints:
1001x03 - 2
1001x03 - 2
It should print :
100x02- 2
1001x03 - 2

import os

list = ['100x02','1001x03']   # list of errors#
path = 'C:\\02'
for root,dirs, files in os.walk(path):
    for ef in  files :
        cur_ef = os.path.join(path,ef)
        if os.path.isfile(cur_ef):
        
            with open (cur_ef,'r') as fto_read  :
                count = 0
                for e_ln in fto_read :           # reading each line from a file #
                    e_ln=e_ln.rstrip()
                    #print (e_ln)
                    for e_el in list :           # reading an element from the list #
                        if e_el in e_ln :
                            e_el=e_el.rstrip()
                            #print ("FOUND LINE --->> ",e_ln)
                            count+=1
                if count > 0:
                    print(f"{e_el} - {count}")
Reply
#9
Bowlofred! You pushed me in the right direction!
I think I fixed my script and it does what I want now.
It finds all the 'error codes' from the list in all my log files and prints them out in the format I want:
3-100x02
2-1001x03
Here is a final version of the snippet:

import os

list = ['100x02','1001x03']   # list of errors#
path = 'C:\\02'
for root,dirs, files in os.walk(path):
    for ef in  files :
        cur_ef = os.path.join(path,ef)
        if os.path.isfile(cur_ef):

            with open (cur_ef,'r') as fto_read  :
                count = 0
                er_nm = []
                for e_ln in fto_read :           # reading each line from a file #
                    for e_el in list :           # reading an element from the list #
                        if e_el in e_ln :
                            e_el=e_el.rstrip()
                            er_nm.append(e_el)
                            count+=1
                if count > 0:            
                    print ("Number of Errors - Name ---- >> ",str(count)+'-'+er_nm[0])

Thank you again for all your help!
I really appreciate it, you guys make my life easier.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Help to find the largest int number in a file directory SalzmannNicholas 1 80 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  count every 28 files and find desire files RolanRoll 3 395 Dec-12-2021, 04:42 PM
Last Post: Axel_Erfurt
  Find the highest value of a list Menthix 4 391 Oct-29-2021, 02:32 PM
Last Post: Menthix
  pandas pivot table: How to find count for each group in Index and Column JaneTan 0 453 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  Have to use Python 2.x to find "yesterday' files tester_V 6 736 Sep-19-2021, 12:26 AM
Last Post: tester_V
  Error in the files last 8 lines Led_Zeppelin 2 746 Sep-11-2021, 09:55 PM
Last Post: snippsat
  Error about missing directory Led_Zeppelin 3 780 Aug-31-2021, 01:37 PM
Last Post: snippsat
  count item in list korenron 8 1,146 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 1,009 Aug-17-2021, 05:24 PM
Last Post: Franky77
  get all the files in the path in a list? korenron 23 2,209 Jul-19-2021, 07:44 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020