Hello! I have a problem with writing the results of the script to a file. In this code snippet:

print 'ip: ', sw.ip_sw.ljust(15), '\tlocation: ', name_adr.split(' ')[-2].rjust(20), '\tports UP: ', str(sw.ports_up).rjust(2), '\tports DOWN: ', str(sw.ports_down).rjust(2), '\tports ALL: ', str(sw.ports_up+sw.ports_down).rjust(3) 

I display the results of the values ​​on the screen. The same is necessary to write to a file. A design of this type:

 fil = open('./portlog', 'w') fil.write(sw.ip_sw, name_adr, sw.ports_up, sw.ports_down, sw.ports_up+sw.ports_down) fil.close() 

Does not work. Tell me what I'm doing wrong.

  • see the answer or type print fil.write .__ doc__ - alexlz
  • And yet, in python3 print is different. - alexlz

2 answers 2

It's not entirely clear what you want to get in your portlog, but maybe it will suit you.

 fil = open('./portlog', 'w') print >>fil, 'ip: ', sw.ip_sw.ljust(15), 'tlocation: ', name_adr.split(' ')[-2].rjust(20), 'tports UP: ', str(sw.ports_up).rjust(2), 'tports DOWN: ', str(sw.ports_down).rjust(2), 'tports ALL: ', str(sw.ports_up+sw.ports_down).rjust(3 

And in your variant to the write method (with one parameter) you are trying to transfer several

  • Thank you so much!) What you need .. fixed and everything worked ... - termitorennet
 fil.write(sw.ip_sw, name_adr, sw.ports_up, sw.ports_down, sw.ports_up+sw.ports_down) 

The syntax is broken. write one parameter.