×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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:

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() 

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

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:
        str_t = ""
        #print (ln)
        ln = ln.rstrip()
        if start_tm in ln:
        ...
        ... 

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

(OP)
to mikrom:
Thank you! it is a good idea!
I'll check it out.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close