473,804 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1524
"Bart Nessux" <ba*********@ho tmail.com> wrote in message
news:c0******** **@solaris.cc.v t.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.readl ines() 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*********@ho tmail.com> schrieb im Newsbeitrag
news:c0******** **@solaris.cc.v t.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.readl ines()

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.readl ines() # 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.app end(inputFile.r ead())

You probably meant something like this (untested):

inputFile = file('ips.txt', 'r')
ip_list = inputFile.readl ines() # 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
6066
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 try to modify the objects in the array while iterating through it. I marked the bugs in this code: // Loop through all previously added accounts foreach(Account a in a1) // a1 is an ArrayList { // If name and context is the same if(a.Name ==...
2
7788
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 unable to find the actual answer... I expected : Array = , "/subdir2", , "/subdir3", ]]
5
1966
by: mikehulluk | last post by:
Ok, Imagine I have a class class C { }; ostream& operator<<(ostream& o, const C& c) { ...}
4
2823
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 great deal of help from gits, I've developed a DOM creation and deletion script, which can be used in multiple applications. You simply feed the script a JSON list of any size, and the script will create multiple DOM elements with as many attributes...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.