NameError: name 'final_error_code' is not defined

Viewed 38

I tried to assign final_error_code as a global variable but still the problem persists.

File "C:\Ujjwal\sample_python_proj - Copy\fetch_array.py", line 154, in get_error_code_array print("Lemon Squeeze", final_error_code) NameError: name 'final_error_code' is not defined


def get_error_code_array():
    array_pt_failed_current = []
    # global final_error_code
    for loop in files:
        # print("This is get error code first loop", loop)
        with open(loop, 'w+') as file:
            # print("This is get error code second loop", loop)
            lines = file.readlines()
            error_str = json.dumps(lines)
            print("This is get error code of ",error_str)
            for pt_found in re.finditer('PT-', error_str):
                i = pt_found.end() + 1
                pt_failed_current = error_str[pt_found.end()] + error_str[i] + error_str[i+1]
                array_pt_failed_current.append(pt_failed_current)
                final_error_code = ["PT-"+ls for ls in array_pt_failed_current]
                # show_error_code_array.append(final_error_code)
    print("Lemon Squeeze", final_error_code)
    return (final_error_code)
1 Answers

final_error_code is defined in your loop.

If it is not defined, that means that your loop didn't loop at all.

Your for loop condition is: re.finditer('PT-', error_str), so I assume that re.finditer(...) didn't find anything.

Related