473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 18 '05 #1
5 1486
"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
"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
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

[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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
2
by: CamelR | last post by:
I have a newbie question, and I have seen reference to this mentioned in many places, but all just say "this has been discussed frequently in (other places) so we won't discuss it here..." and I am...
5
by: mikehulluk | last post by:
Ok, Imagine I have a class class C { }; ostream& operator<<(ostream& o, const C& c) { ...}
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.