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
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 398 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Program to find Mode of a list PythonBoy 6 996 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Function to count words in a list up to and including Sam Oldman45 15 6,407 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Strange argument count error rowan_bradley 3 659 Aug-06-2023, 10:58 AM
Last Post: rowan_bradley
  change directory of save of python files akbarza 3 807 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Coding error. Can't open directory EddieG 6 1,062 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,072 Jun-27-2023, 01:17 PM
Last Post: diver999
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,568 May-07-2023, 12:33 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,012 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  list the files using query in python arjunaram 0 649 Mar-28-2023, 02:39 PM
Last Post: arjunaram

Forum Jump:

User Panel Messages

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