Python Forum
List of error codes to find (and count) in all files in a directory - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List of error codes to find (and count) in all files in a directory (/thread-31419.html)



List of error codes to find (and count) in all files in a directory - tester_V - Dec-10-2020

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)



RE: List of error codes to find (and count) in all files in a directory - bowlofred - Dec-10-2020

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.


RE: List of error codes to find (and count) in all files in a directory - tester_V - Dec-10-2020

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.


RE: List of error codes to find (and count) in all files in a directory - bowlofred - Dec-10-2020

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}")



RE: List of error codes to find (and count) in all files in a directory - tester_V - Dec-10-2020

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!


RE: List of error codes to find (and count) in all files in a directory - bowlofred - Dec-10-2020

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?


RE: List of error codes to find (and count) in all files in a directory - tester_V - Dec-11-2020

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.


RE: List of error codes to find (and count) in all files in a directory - tester_V - Dec-11-2020

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}")



RE: List of error codes to find (and count) in all files in a directory - tester_V - Dec-11-2020

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.