iterating over a list and printing
Peter Otten
__peter__ at web.de
Wed Feb 11 10:38:01 EST 2004
Bart Nessux wrote:
> ip_list = []
> inputFile = file('ips.txt', 'r')
> ip_list.append(inputFile.read())
You are reading the entire file in one big string and append it to the list
which will always have one big item.
> 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...
Assuming the file contains IPs one at a line, either initialize
ip_list = inputFile.readlines()
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
More information about the Python-list
mailing list