iterating over a list and printing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart Nessux

    iterating over a list and printing

    ip_list = []
    inputFile = file('ips.txt', 'r')
    ip_list.append( inputFile.read( ))
    inputFile.close ()
    for i in ip_list:
    print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast
    128.173.123.255 up" %i

    The last line does not work. It prints the first part (/sbin/ifconfig),
    then the entire list of ips, then the second part (netmask 255.255.252.0
    broadcast 128.173.123.255 up). Any ideas on how to fix this? The desired
    results are to print a line for each IP.

    /sbin/ifconfig IP1 netmask 255.255.252.0 broadcast 128.173.123.255 up
    /sbin/ifconfig IP2 netmask 255.255.252.0 broadcast 128.173.123.255 up
    etc...

  • DomF

    #2
    Re: iterating over a list and printing

    "Bart Nessux" <bart_nessux@ho tmail.com> wrote in message
    news:c0dhev$2gp $1@solaris.cc.v t.edu...[color=blue]
    > ip_list = []
    > inputFile = file('ips.txt', 'r')
    > ip_list.append( inputFile.read( ))[/color]

    This line just reads the whole file into the first element of the list.
    Put "print ip_list" here to confirm this to yourself.

    You need to loop over the file with the optional "size" parameter set or
    by using inputFile.readl ines() if ips.txt is one IP address per line.

    Dom
    [color=blue]
    > inputFile.close ()
    > for i in ip_list:
    > print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast
    > 128.173.123.255 up" %i
    >
    > The last line does not work. It prints the first part[/color]
    (/sbin/ifconfig),[color=blue]
    > then the entire list of ips, then the second part (netmask[/color]
    255.255.252.0[color=blue]
    > broadcast 128.173.123.255 up). Any ideas on how to fix this? The[/color]
    desired[color=blue]
    > results are to print a line for each IP.
    >
    > /sbin/ifconfig IP1 netmask 255.255.252.0 broadcast 128.173.123.255 up
    > /sbin/ifconfig IP2 netmask 255.255.252.0 broadcast 128.173.123.255 up
    > etc...[/color]



    Comment

    • vincent wehren

      #3
      Re: iterating over a list and printing

      "Bart Nessux" <bart_nessux@ho tmail.com> schrieb im Newsbeitrag
      news:c0dhev$2gp $1@solaris.cc.v t.edu...
      | ip_list = []
      | inputFile = file('ips.txt', 'r')
      | ip_list.append( inputFile.read( ))

      You just added the entire contents as a single element to ip_list
      To iterate over the indiviual lines, you should go

      for i in file("ips.txt") :
      print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast 128.173.123.255
      up" %i

      HTH,
      Vincent Wehren

      | inputFile.close ()
      | for i in ip_list:
      | print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast
      | 128.173.123.255 up" %i
      |
      | The last line does not work. It prints the first part (/sbin/ifconfig),
      | then the entire list of ips, then the second part (netmask 255.255.252.0
      | broadcast 128.173.123.255 up). Any ideas on how to fix this? The desired
      | results are to print a line for each IP.
      |
      | /sbin/ifconfig IP1 netmask 255.255.252.0 broadcast 128.173.123.255 up
      | /sbin/ifconfig IP2 netmask 255.255.252.0 broadcast 128.173.123.255 up
      | etc...



      |


      Comment

      • Peter Otten

        #4
        Re: iterating over a list and printing

        Bart Nessux wrote:
        [color=blue]
        > ip_list = []
        > inputFile = file('ips.txt', 'r')
        > ip_list.append( inputFile.read( ))[/color]

        You are reading the entire file in one big string and append it to the list
        which will always have one big item.
        [color=blue]
        > inputFile.close ()
        > for i in ip_list:
        > print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast
        > 128.173.123.255 up" %i
        >
        > The last line does not work. It prints the first part (/sbin/ifconfig),
        > then the entire list of ips, then the second part (netmask 255.255.252.0
        > broadcast 128.173.123.255 up). Any ideas on how to fix this? The desired
        > results are to print a line for each IP.
        >
        > /sbin/ifconfig IP1 netmask 255.255.252.0 broadcast 128.173.123.255 up
        > /sbin/ifconfig IP2 netmask 255.255.252.0 broadcast 128.173.123.255 up
        > etc...[/color]

        Assuming the file contains IPs one at a line, either initialize

        ip_list = inputFile.readl ines()

        or entirely omit the intermediate list:
        (untested)

        for line in file("ips.txt") :
        print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast 128.173.123.255
        up" % line.strip()

        strip() removes any leading/trailing whitespace including the newline
        character at the end.

        Peter

        Comment

        • Richie Hindle

          #5
          Re: iterating over a list and printing


          [Bart][color=blue][color=green][color=darkred]
          >>> ip_list.append( inputFile.read( ))[/color][/color][/color]

          You probably meant something like this (untested):

          inputFile = file('ips.txt', 'r')
          ip_list = inputFile.readl ines() # Note readlines() rather than read()
          inputFile.close ()
          for i in ip_list:
          print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast 128.173.123.255 up" %i

          If you really do need to create ip_list up front and append to it,
          you should use extend() rather than append() - see the "mutable
          sequence types" documentation for details.

          --
          Richie Hindle
          richie@entrian. com


          Comment

          • Bart Nessux

            #6
            Re: iterating over a list and printing

            Richie Hindle wrote:[color=blue]
            > [Bart]
            >[color=green][color=darkred]
            >>>>ip_list.app end(inputFile.r ead())[/color][/color]
            >
            >
            > You probably meant something like this (untested):
            >
            > inputFile = file('ips.txt', 'r')
            > ip_list = inputFile.readl ines() # Note readlines() rather than read()
            > inputFile.close ()
            > for i in ip_list:
            > print "/sbin/ifconfig %s netmask 255.255.252.0 broadcast 128.173.123.255 up" %i
            >
            > If you really do need to create ip_list up front and append to it,
            > you should use extend() rather than append() - see the "mutable
            > sequence types" documentation for details.
            >[/color]

            Thanks to everyone for the replies. This works now.

            Comment

            Working...