By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

iterating over a list and printing

P: n/a
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...

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c0**********@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...


Jul 18 '05 #2

P: n/a
"Bart Nessux" <ba*********@hotmail.com> schrieb im Newsbeitrag
news:c0**********@solaris.cc.vt.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...

|
Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

P: n/a

[Bart]
ip_list.append(inputFile.read())


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.

--
Richie Hindle
ri****@entrian.com
Jul 18 '05 #5

P: n/a
Richie Hindle wrote:
[Bart]
ip_list.append(inputFile.read())

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.

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.