Home > Enterprise >  Looking for a string in file via python always fail
Looking for a string in file via python always fail

Time:06-28

I created a function that getting a few string to search in a log file. The function will return True if all the strings are exist in file. else: the function will return False. The function also creating a list of all the missing strings and printing the list.

I used the readlines() function in order to search each string line by line. The file that I'm searching is valid, and theoretically all the strings are in it and the function need to return True. somehow, it always returning false and printing all the strings [which not suppose to happen - if all the strings exist the list should be empty.]

I will attach my code bellow, thanks for the help:


def pre_conditions():
    with open(NR_log, 'r') as logfile:
        name_key = 'Executing script: '   recipe_name
        app_key = 'Application was powered-up successfully, mode is: Review'
        api_key = 'API recipe was chosen'
        lot_key = 'Lot was created successfully'
        recipe_key = 'Recipe execution started'
        wafer_key = 'The Wafer was loaded successfully'
        recipe_pause_key = 'Recipe run is paused'
        program_key = 'Moving to Program mode'
        recipe_creation_key = 'Recipe was saved successfully under the name: sanity_2022-06-22_Ver_5.1'
        lst1 = lst

        for line in logfile.readlines():
            if name_key not in logfile:
                lst.append('\nError: Script was not successfully executed.\n')
            if app_key not in logfile:
                lst.append('\nError: Application was failed to power up.\n')
            if api_key not in logfile:
                lst.append("\nError: Recipe type [API] was not successfully chosen.\n")
            if lot_key not in logfile:
                lst.append("\nError: A lot was not successfully created.\n")
            if recipe_key not in logfile:
                lst.append("\nError: A timeout, recipe was not executed.\n")
            if wafer_key not in logfile:
                lst.append("\nError: The wafer was not loaded.\n")
            if recipe_pause_key not in logfile:
                lst.append("\nError: The recipe was not paused.\n")
            if program_key not in logfile:
                lst.append("\nError: The script was not switch to program key.\n")
            if recipe_creation_key not in logfile:
                lst.append("\nError: The recipe was not saved.\n")
        if not lst:
            return True, print("Pre conditions are OK.")
        return False, pre_conditions_failed_list_print(lst1)


def pre_conditions_failed_list_print(lst1):
    for x in list(OrderedDict.fromkeys(lst1)):
        print(x)


pre_conditions()

CodePudding user response:

Something like this should do the meat of your work I think, I leave the returning of a string etc up to you :).

keys = [name_key, app_key, <fill in your keys>]
found_key = {key: False for key in keys}
for line in logfile.readlines():
    for key in keys:
        if key in line:
            found_key[key] = True

CodePudding user response:

You need to check the lines you read, not the logfile instance

with open(NR_log, 'r') as logfile:
    lines = [line.strip() for line in logfile.readlines()]
    if name_key not in lines:
        lst.append('\nError: Script was not successfully executed.\n')
    ....
  • Related