title: Writing Strings to files in python
body: <p>I'm getting the following error when trying to write a string to a file in pythion:</p>
  
  <pre><code>Traceback (most recent call last):
    File "export_off.py", line 264, in execute
      save_off(self.properties.path, context)
    File "export_off.py", line 244, in save_off
      primary.write(file)
    File "export_off.py", line 181, in write
      variable.write(file)
    File "export_off.py", line 118, in write
      file.write(self.value)
  TypeError: must be bytes or buffer, not str
  </code></pre>
  
  <p>I basically have a string class, which contains a string:</p>
  
  <pre><code>class _off_str(object):
      __slots__ = 'value'
      def __init__(self, val=""):
          self.value=val
  
      def get_size(self):
          return SZ_SHORT
  
      def write(self,file):
          file.write(self.value)
  
      def __str__(self):
          return str(self.value)
  </code></pre>
  
  <p>Furthermore, I'm calling that class like this (where variable is an array of _off_str objects:</p>
  
  <pre><code>def write(self, file):
      for variable in self.variables:
          variable.write(file)
  </code></pre>
  
  <p>I have no idea what is going on.  I've seen other python programs writing strings to files, so why can't this one?</p>
  
  <p>Thank you very much for your help.</p>
  
  <p>Edit:  It looks like I needed to state how I opened the file, here is how:</p>
  
  <pre><code>file = open(filename, 'wb')
  primary.write(file)
  file.close()
  </code></pre>