I am trying to save the output to a file and the indentations are ok.. but I keep getting this error please see below:
This is the error below:
Traceback (most recent call last):
File "command.py", line 36, in <module>
file.write(output)
TypeError: expected a character buffer object
Can someone look at my code below and see what I am doing wrong. I am new to python so any help would be appreciated.
#Importing the necessary module. help
from trigger.cmds import Commando
#Asking the user for input.
devices = raw_input('\nEnter devices separated by comma: ')
commands = raw_input('\nEnter commands separated by comma: ')
#Splitting the devices/commands entered by the user.
devices_list = devices.split(',')
commands_list = commands.split(',')
#Running all given commands on all given devices.
cmd = Commando(devices = devices_list, commands = commands_list)
#Executing all the work.
cmd.run()
#Capturing the results
output = cmd.results
#print output
#Asking the user if they want to print to screen or save to file.
user_option = raw_input('Type "p" to print to screen or "s" to save to file: ')
if user_option == 'p':
print output
elif user_option == "s":
filename = raw_input('Please name your file. Example: /home/ubuntu/bgp.txt: ')
#Print the output to file.
with open(filename, 'w') as file:
file.write(output)
print "Done...Check out %s to see the results." % filename
#End of Program
Please help with this code
Convert output variable to string object using str method
EX:
with open(filename, 'w') as file:
file.write(str(output))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.