iterating over a list and printing

DomF fidtz at clara#spam#.co.uk
Wed Feb 11 16:36:23 CET 2004


"Bart Nessux" <bart_nessux at hotmail.com> wrote in message
news:c0dhev$2gp$1 at solaris.cc.vt.edu...
> ip_list = []
> inputFile = file('ips.txt', 'r')
> ip_list.append(inputFile.read())

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.readlines() if ips.txt is one IP address per line.

Dom

> 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...






More information about the Python-list mailing list