'Write the current ConfigObj as a file
tekNico: FIXME: use StringIO instead of real files
>>> filename = a.filename
>>> a.filename = \'test.ini\'
>>> a.write()
>>> a.filename = filename
>>> a == ConfigObj(\'test.ini\', raise_errors=True)
1'

def write(self, outfile=None, section=None):

    if (self.indent_type is None):
        self.indent_type = DEFAULT_INDENT_TYPE
    out = []
    cs = self._a_to_u('#')
    csp = self._a_to_u('#    ')
    if (section is None):
        int_val = self.interpolation
        self.interpolation = False
        section = self
        for line in self.initial_comment:
            line = self._decode_element(line)
            stripped_line = line.strip()
            if (stripped_line and (not stripped_line.startswith(cs))):
                line = (csp + line)
            out.append(line)
    indent_string = (self.indent_type * section.depth)
    for entry in (section.scalars + section.sections):
        if (entry in section.defaults):
            continue
        for comment_line in section.comments[entry]:
            comment_line = self._decode_element(comment_line.lstrip())
            if (comment_line and (not comment_line.startswith(cs))):
                comment_line = (csp + comment_line)
            out.append((indent_string + comment_line))
        this_entry = section[entry]
        comment = self._handle_comment(section.inline_comments[entry])
        if isinstance(this_entry, dict):
            out.append(self._write_marker(indent_string, this_entry.depth, entry, comment))
            out.extend(self.write(section=this_entry))
        else:
            out.append(self._write_line(indent_string, entry, this_entry, comment))
    if (section is self):
        for line in self.final_comment:
            line = self._decode_element(line)
            stripped_line = line.strip()
            if (stripped_line and (not stripped_line.startswith(cs))):
                line = (csp + line)
            out.append(line)
        self.interpolation = int_val
    if (section is not self):
        return out
    if ((self.filename is None) and (outfile is None)):
        if self.encoding:
            out = [l.encode(self.encoding) for l in out]
        if (self.BOM and ((self.encoding is None) or (BOM_LIST.get(self.encoding.lower()) == 'utf_8'))):
            if (not out):
                out.append('')
            out[0] = (BOM_UTF8 + out[0])
        return out
    newline = (self.newlines or os.linesep)
    output = self._a_to_u(newline).join(out)
    if self.encoding:
        output = output.encode(self.encoding)
    if (self.BOM and ((self.encoding is None) or match_utf8(self.encoding))):
        output = (BOM_UTF8 + output)
    if (not output.endswith(newline)):
        output += newline
    if (outfile is not None):
        outfile.write(output)
    else:
        h = open(self.filename, 'wb')
        h.write(output)
        h.close()
