(New) String problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Charlie of Bolton
    New Member
    • Mar 2007
    • 32

    (New) String problem

    Hi,

    I would like to know why, Idle is telling me that the IP is
    not defined, here is my error msg from Python Idle:

    Code:
    Traceback (most recent call last):
      File "E:/Charlie Python/11 Octo 2007/test5.txt", line 62, in <module>
        print 'loss = ' + loss + ip
    NameError: name 'ip' is not defined

    And here is my code:

    Code:
    while not action:
        pingaling =os.popen("ping %s" %(ipAddress),"r")
        results = pingaling.read() 
        print results
        for line in results:
            f=open('c:/tmp/workfile.txt', 'a+')
            f.write(line)
            f.close()
        for line in open("c:/tmp/workfile.txt"): 
            s = 0
            e = 0
            loss_num = 0 # Initialize it
        if line.startswith("Ping statistics for"):
            ip=line.split()[-1][:-1]
        if line.find("(")!=-1:
            s=line.index("(")
            e=line.index(")")
            loss=line[s+1:e].replace("loss","")
    #------------------------------------------------------
            print 'loss = ' + loss + ip
            loss_num = loss.strip() # remove spaces if any
            loss_num = loss_num.strip('%') # remove the % sign from loss       
            print 'number = ' + loss_num
        try:
                  loss_num = float(loss_num) # change it into a number
                  if loss_num >= 2 :
                   output_line = loss + ',' + "'" + ip + "'" + "\n"
                   s=open('c:/tmp/myprimarylogs.txt', 'a+')
                  #s=open('c:/tmp/primaryip.cvs', 'a+')
                   s.write(output_line)
                  #s.write(line+"\n")
                   s.close()
        except:
                   loss_num = ''         
        print ('-----------------')
        print ('Please wait')
        print ('-----------------') 
        # waiting
        time.sleep(1)
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    It's not IDLE giving you the message. IDLE is simply the interface that you are using to connect to the Python interpreter. You would get this message no matter how you connect to Python. Because:
    I don't see
    Code:
    ip = anything
    anywhere. It's got to be assigned in order to be used. Perhaps you want
    Code:
     + ipAddress
    in there.
    Last edited by bartonc; Oct 11 '07, 09:38 PM.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by Charlie of Bolton
      Hi,

      I would like to know why, Idle is telling me that the IP is
      not defined, here is my error msg from Python Idle:

      Code:
      Traceback (most recent call last):
        File "E:/Charlie Python/11 Octo 2007/test5.txt", line 62, in <module>
          print 'loss = ' + loss + ip
      NameError: name 'ip' is not defined

      And here is my code:

      Code:
      while not action:
          pingaling =os.popen("ping %s" %(ipAddress),"r")
          results = pingaling.read() 
          print results
          for line in results:
              f=open('c:/tmp/workfile.txt', 'a+')
              f.write(line)
              f.close()
          for line in open("c:/tmp/workfile.txt"): 
              s = 0
              e = 0
              loss_num = 0 # Initialize it
          if line.startswith("Ping statistics for"):
              ip=line.split()[-1][:-1]
          if line.find("(")!=-1:
              s=line.index("(")
              e=line.index(")")
              loss=line[s+1:e].replace("loss","")
      #------------------------------------------------------
              print 'loss = ' + loss + ip
              loss_num = loss.strip() # remove spaces if any
              loss_num = loss_num.strip('%') # remove the % sign from loss       
              print 'number = ' + loss_num
          try:
                    loss_num = float(loss_num) # change it into a number
                    if loss_num >= 2 :
                     output_line = loss + ',' + "'" + ip + "'" + "\n"
                     s=open('c:/tmp/myprimarylogs.txt', 'a+')
                    #s=open('c:/tmp/primaryip.cvs', 'a+')
                     s.write(output_line)
                    #s.write(line+"\n")
                     s.close()
          except:
                     loss_num = ''         
          print ('-----------------')
          print ('Please wait')
          print ('-----------------') 
          # waiting
          time.sleep(1)
      It is likely that the if statement below is returning False:[code=Python]
      if line.startswith ("Ping statistics for"):
      ip=line.split()[-1][:-1][/code]You have already iterated on the data file. The file method read() returns a single string.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bvdet
        It is likely that the if statement below is returning False:[code=Python]
        if line.startswith ("Ping statistics for"):
        ip=line.split()[-1][:-1][/code]You have already iterated on the data file. The file method read() returns a single string.
        I missed that due to the missing space between 'ip' and '='. I could have refined my search or people could get in the practice of producing readable code... Hmmm.

        Comment

        Working...