Python Forum
have problem printing after ‘else’
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
have problem printing after ‘else’
#11
A flag is just a variable that holds (and is tested) for a single condition. Like in your case you want to know if a word is found in a file. So your flag might be: match_found.

Before you start on the file, you haven't found anything yet, so you'll set the variable to false.

match_found = False
As you work on the file, you'll discard any lines that don't match. They won't matter. But if you do find a matching line, you'll print it out, and you'll also change the flag. If you find the line 20 times in the file, you'll set the flag 20 times. But that doesn't matter.

match_found = True
After you're done with the file, you now have an indicator as to whether or not you found anything. If you did, the flag is set to true and you go to the next file.

If the flag is false, you never found anything and you can do your "no match found" processing.

if match_found:
    pass # don't need to print anything else if it's found?
else:
    print("No match in this file.")
Reply
#12
I have a file it has 5000 or 100000 lines, does not matter how many...
one line in the file has the word "FIND".


For each line that does not have the word "FIND" the code will " print("No match in this file.")"
it will print 4999 lines "No match in this file."
that is what I'm trying to avoid, printing 4999 lines.
I want to print one time "No. match in this file." not 4999 times.
And that is a question from the beginning.
                if find1 in ln_f :
                    er_f.write(ln_f)
                    print (dir_f+ '-'+ find1)                
                else :
                    notin_01 = (dir_f + "-" + "Cannot FIND - "+ "-"+ find1)
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem printing last element from a list tester_V 3 2,387 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