code: def tofile(self, fobj, format):
          
          if format == 'hex':
              self.write_hex_file(fobj)
          elif format == 'bin':
              self.tobinfile(fobj)
          else:
              raise ValueError('format should be either "hex" or "bin";'
                  ' got %r instead' % format)
docs: Write data to hex or bin file. Preferred method over tobin or tohex.
  
          @param  fobj        file name or file-like object
          @param  format      file format ("hex" or "bin")