0

I'm scanning log files (in a directory) for specific lines, need to output all in a text file.

For some reason, I do not understand, I have an error:

NameError: name 's' is not defined

I tried to assign Var s to a different Var but still cannot get around the problem.

In my code, I going to have one "if" and 5 "elif" like this:

If
   var1
elif
   var2
elif
   var3
elif
   var4
elif
   var5
elif
   var6
write.output_file(var1+var2+var3+var4+var5+var6+'\n')

Here is the code:

import os
import re

start_tm  = ',"  Start Time'
test_ID   = ' Host Name: '

cell_f = open('C:\\03\\OUTCOME.txt','w')
given_path = 'C:\\02\\en15\\TST2'


for filename in os.listdir(given_path):
    filepath = os.path.join(given_path, filename)
    if os.path.isfile(filepath):
        #print("File Name:   ", filename) 
        print("File Name\\Path:", filepath+'\n') 
        with open(filepath) as mfile:        
            for rn_l in mfile:
                rn_l= rn_l.rstrip()

                if start_tm  in rn_l:
                    *extraWords,st_t1 = rn_l.split('Time') # Grtting Start Time #
                    s=str(st_t1)
                    s=s.replace('"','')

                    print ("Start String \"s\"is a TYPE  ", type(s))
                    print ("START TIME STRING "+s)

                elif test_ID  in rn_l:                      
                    *extraWords,t_id1,t_id2, = rn_l.split( )                                 
                    print ("TESTER ID --->>> "+t_id1)                                       
                    t_id11,t_id12, = rn_l.split('Host') 

                    *w,e,c,d = re.split(" +", t_id11)
                    cell = str(e+'_'+c)
                    cell = cell.replace('[','').replace(']','')
                    print ("Cell Line " + cell+'\n')
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Vlade Past
  • 17
  • 3
  • which line did the error msg indicate? was it the one within second if statement? or down the road where for exists and do something else? – Michael Hsi Apr 18 '20 at 06:28
  • As soon as the print ( the print statement at the ens of the script) is enabled, the script produces this error: print (s + cell+'\n') NameError: name 's' is not defined – Vlade Past Apr 18 '20 at 06:36

1 Answers1

1

ok, so the reason this happens, is because when

(start_tm in rn_l) == True

python will enter the if statement, which will assign some value to s. After which, python ignores the elif statement and exit out of the if block and continue to whatever is after if.

when

(start_tm in rn_l) != True 

python checks the condition in elif, and if that statement is True,

(test_ID in rn_1) == True

python will not enter if block, instead enters the elif block, which does NOT assign value to s. That's why you're getting NameError


Correction:

Even though the above explanation is correct, it does not address the for loop issue. This error can only occur when elif is entered before if. Because, if python enters if first, then there should be a s variable, however if python enters elif it will throw that error, because if wasn't entered prior.

In essence, this happens because the first line of your file cannot satisfy your if statement. There are a couple of ways you can handle this. If you cannot guarantee start_tm in rn_l is True for the first line of the file, and you still need those lines that does not satisfy start_tm in rn_l, define s as empty string or something before entering for loop.

If you can ditch those lines, define s as None, and check in elif so that if s is None (that means the if hasn't been entered yet), do not execute elif.

See

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Michael Hsi
  • 439
  • 2
  • 8
  • Thank you, I really appreciate it that is amazing to me! should I use IF in the next block instead of elif? What is a proper Python way? – Vlade Past Apr 18 '20 at 06:48
  • Just changed elif to if still same error; print (s + cell+'\n') NameError: name 's' is not defined. when the print hashed I have no errors and each bloc prints the Vars – Vlade Past Apr 18 '20 at 06:50
  • I did more reading on the "you can ditch those lines, define s as None" and the script started printing! That progress! Thank you (not sure who!) Patrick and Michael! And Todd! – Vlade Past Apr 19 '20 at 06:39
  • Totaly working! I did not know about " define s as None"! Thank you again! – Vlade Past Apr 19 '20 at 06:42
  • np Vlade, glad the answer helped : ) – Michael Hsi Apr 19 '20 at 18:58