0

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

  • 1
    Very common error. Did you search for previous questions and answers? https://stackoverflow.com/q/9786941/1531971 –  Jan 19 '18 at 16:14
  • 2
    Possible duplicate of [TypeError: expected a character buffer object - while trying to save integer to textfile](https://stackoverflow.com/questions/9786941/typeerror-expected-a-character-buffer-object-while-trying-to-save-integer-to) – Spoody Jan 19 '18 at 16:14

1 Answers1

0

Convert output variable to string object using str method

EX:

with open(filename, 'w') as file:
    file.write(str(output))
Rakesh
  • 81,458
  • 17
  • 76
  • 113