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
  get all the files in the path in a list? korenron 23 774 Jul-19-2021, 07:44 AM
Last Post: korenron
  count item in list korenron 7 299 Jul-01-2021, 01:46 PM
Last Post: deanhystad
  find 'yesterdays files' tester_V 8 535 Jun-18-2021, 02:10 AM
Last Post: tester_V
  Find and replace in files with regex and Python Melcu54 0 373 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  Find Common Elements in 2 list quest 4 610 Apr-14-2021, 03:57 PM
Last Post: quest
  list.count does not appear to function Oldman45 7 926 Mar-16-2021, 04:25 PM
Last Post: Oldman45
  Function to count words in a list up to and including Sam Oldman45 13 1,178 Feb-18-2021, 10:22 AM
Last Post: perfringo
  Rename Multiple files in directory to remove special characters nyawadasi 9 813 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  How to merge all the files in a directory in to one file sutra 3 637 Dec-10-2020, 12:09 AM
Last Post: sutra
  Merging all file_name.log's files from directory to one and search “PerformanceINFO" sutra 0 389 Dec-09-2020, 05:14 PM
Last Post: sutra

Forum Jump:

User Panel Messages

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