Girls.Gone.Wired
Girls.Gone.Wired

Reputation: 39

Use if False and print without stopping Loop

I have defined a "value" and I have an IF statement such as:

value = any ("1" in valueMaj for valueMaj in list)
if value == False:

Can I make it so my print statement prints out ALL that are not true, not just one. Example:

INPUT

print("FILE NOT MATCH:", file) 

OUTPUT

FILE NOT MATCH: filenumber1.txt

But, I would like it to print all that do not match rather than stopping the loop.

Upvotes: 1

Views: 69

Answers (2)

Girls.Gone.Wired
Girls.Gone.Wired

Reputation: 39

import shutil
import os
import subprocess
import sys
import glob
import re
import fileinput

root_dir = os.getcwd()
uop_dir = root_dir + "\\UoP"

  # .h files
  print ("Searching for .h files: " + uop_dir)
  for root, subfolders, files in os.walk(uop_dir):
   if root.endswith("WHATEVER YOU WANT HERE"):
   if myfile.endswith(".h"):
      print (root + "/" + myfile)
      for file in myfiles:
      for line in open(file):
         line = line.rstrip()
         if re.search('VERSION\s+("\d+\.\d+")$', line):
            version = re.findall("\d+\.\d+" , line)
            # version.append(file)
            if re.search('VERSION\s+("\d+\.\d+")$', line):

                    valueMaj = re.findall('\d+\.', line)
                    ###print(valueMaj)
                    #FINDS MINOR NUMBER
                    if re.search('VERSION\s+("\d+\.\d+")$', line):
                            valueMin = re.findall('\.\d+' , line)
                         ###print(valueMin)
                            value = [valueMaj for valueMaj in file if ('1.7') not in valueMaj]

                            if value:
                                    ###print("FILE NOT MATCH:", file, "\nROOT: " root, "\nVERSION: ", *version)
                                    print("FILE NOT MATCH:", file , "\nVERSION: ", version, "\nMAJOR_NUMBER:", (valueMaj),
                                           "\nMINOR_NUMBER: ", ''.join(valueMin)) 

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121962

Don't use any() if you need all values that do not match. Create a list instead using a list comprehension with a filter:

not_matching = [valueMaj for valueMaj in some_list if '1' not in valueMaj]
if not_matching:
    print('The following files do not match:', ', '.join(not_matching))

or use a loop to print out individual files.

any() will short-circuit and stop iterating; it is only useful for efficient testing of a condition against a sequence, not for filtering that sequence.

Upvotes: 3

Related Questions