Python Forum
have problem printing after ‘else’
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
have problem printing after ‘else’
#1
Hi,
I have 100mb files and I’m looking for some lines (find1, find2, find3 and so on) in each file.
I’d like to print out to a file:
‘file name’ + find1 (if found) ------- I do not have problem doing it.
I have a problem with ‘else’,
I’d like to print to a file:
‘file name +find1+ not Found’ – one time.
I do not want to print ‘file name +find1+ not Found’ each time “foun1” not found in a line.
I cannot 'break' after first find1 is not found, I understand the code will exit from the 'for" loop.
I thought I could use veiable "notin_01 = '' ", I could redirect (and overwrite) all "not found" lines in to it and print the last 'not found' line. For some reason I cannot.
I tried to move
                print (notin_01)
                er_f.write(notin_01)
left and right but could not find a solution.
Thank you

import os
import os.path
import re

logDir = 'Some/Dir/'                       
er_f   = open('other/Dirs/Errors_By_Logs.txt','w')                   
notin_01 = ''
find1= 'find 1'

for fn in os.listdir(logDir):
    if os.path.isfile(os.path.join(logDir, fn)):
        print("     File name : ", fn)                
        dir_f = logDir+fn       
        
        with open(dir_f) as each_dirf :
            for ln_f in each_dirf :
                ln_f=ln_f.rstrip()

                if find1 in ln_f :
                    er_f.write(ln_f)
                    print (dir_f+ '-'+ find1)                
                else :
                    notin_01 = (dir_f + "-" + "Cannot FIND - "+ "-"+ find1)
                print (notin_01)
                er_f.write(notin_01)
er_f.close()
           
Reply
#2
Inside the loop where you read the file, create a sentinel that is initially set to the value that nothing has been found. Something like match_found = False

If you find a match, besides printing the match, you want to change the sentinel to show that a match was found.

After the end of the file loop, check the sentinel. If nothing was found, you can print your message.
Reply
#3
I do not get it "match_found = False"
if the "IF" block is = true print found
"ELSE" is = false print not found
what is does "match_found = False"?
I do not see where and how I can use it.

thank you.
Reply
#4
As bowlofred says, you need a counter or flag.
not_printed_yet = True
for i in range(100):
    if i % 2 == 0:
        # Will get here many times
        if not_printed_yet:
            # Only get here once
            print(i)
            not_printed_yet = False
Reply
#5
I understand you guys think it is clear and maybe it is but Id o not have nested loops:

    if i % 2 == 0:
        # Will get here many times
        if not_printed_yet:
            # Only get here once
            print(i)
            not_printed_yet = False
I already have 'IF' true do this "else" false do this. totally confused about not_printed_yet = False/True

Than more I look than more I become confused.
What does it mean?
not_printed_yet = True
for i in range(100):
    if i % 2 == 0:
        # Will get here many times
        if not_printed_yet:
            # Only get here once
            print(i)
            not_printed_yet = False
if i==0
when the "not_printed_yet =True" become False
Reply
#6
I write you a note that says "Call your mom and wish her a happy birthday". You are busy so you put the note in your pocket as a reminder to call her later. Throughout the day you feel the note in your pocket, pull it out and am reminded that you need to call your mom. Eventually you call her and tear up the note.

In my example "not_printed_yet" is the note to call you mom. In the example it reminds the code that it needs to print a message. Once the message is printed the code "tears up the note" by changing the value of not_printed_yet from False to True. This should be very obvious if you pretend you are a computer and walk through the example.

I don't know why you are talking about "nested loops". Your example code has lots of nested loops. My example does not have any nested loops. But a loop being nested or not has no affect on my proposed solution. It works for all cases. Just like a note to call your mom.
Reply
#7
It is a nice story!
I understand what you are saying but I do not know how implement it.
I already checking if a line is "True" or "False" by having "IF" "else"
"After the end of the file loop, check the sentinel. If nothing was found, you can print your message." - what does it mean? I have already check it.
Reply
#8
I do not understand how to work with the "flags" or what they are Wall .
I think I found solution, probably it is not as good as "Flags" whatever they are, I had to declare vars, I call them "global vars" for each "ELSE" block. I also have to print outside the "ELSE", I do not like it for some reason.
Anyway, just wanted to share what I found with all of you.
Thank you.
find1= 'FIND'
notin_01 =''

for fn in os.listdir(logDir):
    if os.path.isfile(os.path.join(logDir, fn)):               
        dir_f = logDir+fn       
        
        with open(dir_f) as each_dirf :
            for ln_f in each_dirf :
                ln_f=ln_f.rstrip()

                if find1 in ln_f :
                    er_f.write(ln_f+'\n')
                    print (dir_f+ '-'+ ln_f)                
                else :
                    notin_01 = (dir_f + "-" + "Cannot FIND - "+ "-"+ find1)

            er_f.write(notin_01+'\n')
            print (notin_01)
er_f.close()
           


If anybody knows how to use flags, I still will be very interested to know how to use them.
Thank you again.
Reply
#9
It would be hard to be simpler or clearer, so I think any confusion must be from this not being the solution to your problem. So what is your problem? Time for some requirements gathering so you can actually solve the correct problem.

When the pattern is not in the file, what should the program do?
1. Print message and quit?
2. Print message and continue?
3. Write message to log file and continue?
4. Silently continue?
5. Something else?
Reply
#10
I have a lot of 100BM files.
I'll open each file and will search for a line called "FIND", actually 12 more other lines to find, "if FIND in line : ..." statements.
If the line "FIND" found, I'll print the line to a different file. No problem here.
I have a problem with ‘else’.
I’d like to print to a file:
"Line 'FIND’ not in the file" – one time.
I do not want to print ‘Line"FIND" not in the file' each time line “FIND” not found in a line or my output file become 100MB.
My output file will look like this:
line "FIND"
line "FIND"
line "FIND"
LINE "FIND2" not in the file
LINE "FIND3" not in the file


line "BLUE"
line "BLUE2"
line "BLUE2" not in thefile

And so on...

Sorry for the confusion and not the best description of the problem I have.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem printing last element from a list tester_V 3 2,416 Oct-30-2020, 04:54 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

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