Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print produces an error if placed after forth 'IF" block 1

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
0
0
US
Hi,
I have a problem with printing.
If I place a print statement after the forth 'IF' statement I have an error (see below)
If I place a print statement after the third 'IF' statement - no errors
Here is the error:
print ("All Lines -->> ", str_t, endt_p, rnt_p, prg_np, '\n')
NameError: name 'str_t' is not defined

Here is the code:

Python:
import os
import re

runtime_l = ',"  Run  Time'
start_tm  = ',"  Start Time'
end_tm    = ',"  End  Time'
program_n = ',"    Test Program Name:'

ftow = open('C:\\01\\sorted_OUT.txt','w')

with open('C:\\02\\en15\\TST2\\1.txt','r') as ftor:
    for ln in ftor:
        #print (ln)
        ln = ln.rstrip()
        if start_tm in ln:
            
            *extraWords,st_t1 = ln.split('Time') # Grtting Start Time line #
            st_t1=st_t1.replace('"','')
            str_t = st_t1
            #print ("Start Time Line -->> " , str_t)
            
        if end_tm in ln:
            #print (ln) 
            *extraWords,endt1 = ln.split('Time') # Grtting END Time line #
            endt1=endt1.replace('"','')
            endt_p = endt1
            #print ("ENd Time line -->> " , endt1)            
 
        if runtime_l in ln:
            #print (ln) 
            *extraWords,rnt_1 = ln.split('Time') # Grtting Run Time line #
            rnt_1=rnt_1.replace('"','')
            rnt_p = rnt_1
            #print ("Run Time line -->> " , rnt_1)  
            
            print ("All Lines -->> ", str_t, endt1, rnt_1, '\n')        # NO errors if enabled Print here #
        
        if program_n in ln :

            *extraWords,prg_n = ln.split('Name') # Grtting program name line #            
            prg_n=prg_n.replace('"','')
            prg_np = prg_n
            print ("Program Name -->> " ,prg_np)
            print ("All Lines -->> ", str_t, endt_p, rnt_p, prg_np, '\n')   # Produces an error#
            

ftow.close()
 
 printing stops on a 3d if statement
You define str_t in the first if-block and try to print it in the last if-block.
If your program doesn't go in the first if-block then the variable str_t is not defined in the rest of your program.
Try to define it as empty string before, like:
Code:
    for ln in ftor:
        [highlight #FCE94F]str_t = ""[/highlight]
        #print (ln)
        ln = ln.rstrip()
        if start_tm in ln:
        ...
        ...
 
to mikrom:
Thank you! it is a good idea!
I'll check it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top