code: def write(self, data):
          
          if self._closed:
              raise ValueError("TxMongo: cannot write to a closed file.")
  
          try:
              # file-like
              read = data.read
          except AttributeError:
              # string
              if not isinstance(data, (bytes, unicode)):
                  raise TypeError("TxMongo: can only write strings or file-like objects.")
              if isinstance(data, unicode):
                  try:
                      data = data.encode(self.encoding)
                  except AttributeError:
                      raise TypeError("TxMongo: must specify an encoding for file in "
                                      "order to write {0}".format(data))
              read = StringIO(data).read
  
          def do_write(_=None):
              to_write = read(self.chunk_size)
              if to_write and len(to_write) == self.chunk_size:
                  return self.__flush_data(to_write).addCallback(do_write)
              else:
                  self._buffer.write(to_write)
  
          if self._buffer.tell() > 0:
              # Make sure to flush only when _buffer is complete
              space = self.chunk_size - self._buffer.tell()
              if space:
                  to_write = read(space)
                  self._buffer.write(to_write)
                  if len(to_write) < space:
                      return defer.succeed(None)  # EOF or incomplete
              return self.__flush_buffer().addCallback(do_write)
          else:
              return defer.maybeDeferred(do_write)
docs: Write data to the file. There is no return value.
  
          `data` can be either a string of bytes or a file-like object
          (implementing :meth:`read`).
  
          Due to buffering, the data may not actually be written to the
          database until the :meth:`close` method is called. Raises
          :class:`ValueError` if this file is already closed. Raises
          :class:`TypeError` if `data` is not an instance of
          :class:`str` or a file-like object.
  
          :Parameters:
            - `data`: string of bytes or file-like object to be written
              to the file