Writing Strings to files in python

Posted by Leif Andersen on Stack Overflow See other posts from Stack Overflow or by Leif Andersen
Published on 2010-03-22T19:58:51Z Indexed on 2010/03/22 20:01 UTC
Read the original article Hit count: 158

Filed under:
|
|
|

I'm getting the following error when trying to write a string to a file in pythion:

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

I basically have a string class, which contains a string:

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)

Furthermore, I'm calling that class like this:

def write(self, file):
    for variable in self.variables:
        variable.write(file)

I have no idea what is going on. I've seen other python programs writing strings to files, so why can't this one?

Thank you very much for your help.

© Stack Overflow or respective owner

Related posts about python

Related posts about string