[Tutor] passing a variable ?

Kent Johnson kent37 at tds.net
Thu Oct 19 15:37:57 CEST 2006


dpotter at nc.rr.com wrote:
> I am stumped.  I am trying to pass the variable 'savename' to a string
> and get errors.  
> can someone tell me why?  Thanks to all on this list.  (The problem
> happens in the for loop 1st line)
> 
> This is my error
> Traceback (most recent call last):
>   File "./ArpAcl.py", line 39, in ?
>     fout = file('/home/cable/sandbox/%s', 'a''w' % savename)
> TypeError: not all arguments converted during string formatting

You are applying the formatting operator to the string 'aw' not to the 
path string. Try this:
fout = file('/home/cable/sandbox/%s' % savename, 'a''w')

Not sure why you have both 'a' and 'w' - which one do you want? Seems 
like you should pick one.

Kent

> 
> ----------------------------------------------------------------------------
> 
> import pexpect
> import getpass
> import sys
> 
> #Go through all the routers and gather Access-list or ARP information,
> return the info into it's own file.
> print '\n'
> print 'Router scrub will take approx 60 Seconds.'
> print '\n'
> print "For the ARP list enter '1'"
> print "For the ACL list enter '2'"
> print '\n'
> 
> choice = input('Enter 1 or 2: ')
> print '\n'
> telpass = getpass.getpass('Please enter the telnet password: ')
> enablepass = getpass.getpass('Please enter the enable password: ')
> 
> if choice == 1:
>         command = 'show arp'
>         savename = 'arp.txt'
> else:
>         command = 'sh ip access-l'
>         savename = 'acl.txt'
> 
> print '\n'
> print '-' * 48
> print 'You will be notified when program is finished.'
> print '-' * 48
> 
> x = open('/home/cable/router.list','r')
> routers = x.readlines()
> 
> for i in routers:
>         fout = file('/home/cable/sandbox/%s', 'a''w' % savename)
>         c = pexpect.spawn('/usr/bin/telnet %s' %i)
>         c.expect('Please Enter Password:')
>         c.sendline(telpass +'\r')
>         c.sendline('enable\r')
>         c.expect('Password:')
>         c.sendline(enablepass + '\r')
>         c.expect('#')
>         c.logfile = fout
>         c.sendline('skip\r')
>         c.expect('#')
>         c.sendline(command + '\r')
>         c.expect('#')
>         fout.close()
>         c.close()
> x.close()
> print '\n'
> print 'Finished!'
> print '\n'
> print 'File has been save to /home/cable/sandbox/%s' % savename
> raw_input('Press any key to exit....')
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list