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
  Read directory listing of files and parse out the highest number? cubangt 4 98 8 hours ago
Last Post: cubangt
  read a text file, find all integers, append to list oldtrafford 12 743 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  For Word, Count in List (Counts.Items()) new_coder_231013 6 736 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  find some word in text list file and a bit change to them RolanRoll 3 421 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to download a list of files from FTP? schnarkle 0 270 Jun-21-2022, 10:35 PM
Last Post: schnarkle
  How to find the second lowest element in the list? Anonymous 3 448 May-31-2022, 01:58 PM
Last Post: Larz60+
  How to get unique entries in a list and the count of occurrence james2009 5 1,804 May-08-2022, 04:34 AM
Last Post: ndc85430
  How to save files in a separate directory Scordomaniac 3 741 Mar-16-2022, 10:17 AM
Last Post: Gribouillis
  Error in find pearson correlation function erneelgupta 1 664 Mar-01-2022, 03:41 PM
Last Post: stevendaprano
  Python Program to Find the Total Sum of a Nested List vlearner 8 1,320 Jan-23-2022, 07:20 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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