iterating over a list and printing

B

Bart Nessux

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

DomF

Bart Nessux said:
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
 
P

Peter Otten

Bart said:
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
 
V

vincent wehren

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



|
 
R

Richie Hindle

[Bart]
You probably meant something like this (untested):

inputFile = file('ips.txt', 'r')
ip_list = inputFile.readlines() # 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.
 
B

Bart Nessux

Richie said:
[Bart]


You probably meant something like this (untested):

inputFile = file('ips.txt', 'r')
ip_list = inputFile.readlines() # 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.

Thanks to everyone for the replies. This works now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top