1

I have a function that outputs a list of IPs.

def convertHostNamesToIps(hostnames):
    ip = server.system.search.hostname(sessionKey, hostnames )
    for ips in ip:
        print (ips.get('ip'))

The output usually looks like Below are the IPs for CSDP_LAB_STAGING

172.29.219.123
172.29.225.5
172.29.240.174
172.29.225.46
172.29.240.171
172.29.240.175
172.29.219.119
172.29.219.117
172.29.219.33
172.29.219.35
.
.
.
172.29.219.40
172.29.219.35
172.29.219.40
172.29.219.118
172.29.219.121
172.29.219.115
172.29.225.51

Now I want to write this output to file.

What I have done is

def convertHostNamesToIps(hostnames):
    ip = server.system.search.hostname(sessionKey, hostnames )
    sys.stdout=open("test.txt","w")
    for ips in ip:
        print (ips.get('ip'))
    sys.stdout.close()

But the above code only writes the very last IP to test.txt. I thought I might be messing up the indentation but that hasent helped me. Is there something else I am missing ?

P.S. This is my first ever python script so forgive me if Ive done something extremely stupid.

  • 2
    This is overcomplicated. Please read the Python documentation about the `open` function and file objects in general (especially, the `write` method). – ForceBru Mar 31 '17 at 18:56
  • 1
    What @ForceBru said. this should be done through `open` by itself. but if you want to ignore all warnings everyone else will give you for messing directly with sys.stdout..... just put `sys.stdout.flush()` right above `sys.stdout.close()` – TehTris Mar 31 '17 at 19:03

4 Answers4

1

Reassigning sys.stdout? That's... brave.

You can assign the open file to some other variable, then call its write method. If you want stuff on separate lines, you'll have to add those yourself.

def convertHostNamesToIps(hostnames):
    ip = server.system.search.hostname(sessionKey, hostnames )
    my_file=open("test.txt","w")
    for ips in ip:
        my_file.write(ips.get('ip')+'\n')
    my_file.close()
Kirill
  • 449
  • 3
  • 11
1

I went through everyones response above and tried each one out. But every solution led to only the last IP being printed to file. Reading the documentation brought me to the conclusion that I need to append to file rather than write to it.

def convertHostNamesToIps(hostnames):
    ip = server.system.search.hostname(sessionKey, hostnames )
    my_file=open("test.txt","a")
    for ips in ip:
        my_file.write(ips.get('ip')+'\n')
    my_file.close() 
  • Then that means your function gets called repeatedly, and the list of IPs in the last call only contained one IP. But there is no way to deduce that from your question. – tripleee May 07 '21 at 03:29
0

I don't even know how the last IP was saved because your function doesn't have any write in it. You can try this :

def convertHostNamesToIps(hostnames):
    ip = server.system.search.hostname(sessionKey, hostnames )
    list_ips = str ()
    for ips in ip:
    list_ips = list_ips + ips.get('ip') + '\n'

with open ('test.txt', 'w') as file:
    file.write (list_ips)

You need something like file.write () to save your ips. I put all the ips in a string so that they are simpler to save in the file. The with block doesn't require any close function

EDIT (I can't comment) The difference between these two methods:

my_file = open ('test.txt', 'w')

and

my_file = open ('test.txt', 'a')

is only that in the first one, all that was in the file before that function call is executed will be deleted. With append, it will not, and the my_file.write(something_to_add) will be added to the end of the file. But opening in'w' mode will erase the file at the execution of this precise line only I tested myself, this works with 'w' as well as with 'a'

Guil23
  • 191
  • 14
0
def convertHostNamesToIps(hostnames):
ip = server.system.search.hostname(sessionKey, hostnames )
iplist = [] # Make a temporal list.
for ips in ip:
    print (ips.get('ip')) # This only print your ips, it isn't necesary.
    iplist.append(ips.get('ip')) # Add the current ip in the list.
with open("test.txt","w") as txt: # Open the file but when you finish to use the file it will automatically close.
    txt.writelines(iplist)

I hope this will help you.

Ender Look
  • 2,303
  • 2
  • 17
  • 41