<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write(self, outfile):
 """Write this shape list to a region file. Parameters outfile : str File name """

        if len(self) < 1:
            print("WARNING: The region list is empty. The region file "
                  "'{:s}' will be empty.".format(outfile))
            try:
                outf = _builtin_open(outfile, 'w')
                outf.close()
                return
            except IOError as e:
                cmsg = "Unable to create region file '{:s}'.".format(outfile)
                if e.args:
                    e.args = (e.args[0] + '\n' + cmsg,) + e.args[1:]
                else:
                    e.args = (cmsg,)
                raise e

        prev_cs = self[0].coord_format

        outf = None
        try:
            outf = _builtin_open(outfile, 'w')

            attr0 = self[0].attr[1]
            defaultline = " ".join(["{:s}={:s}".format(a, attr0[a])
                                    for a in attr0 if a != 'text'])

            # first line is globals
            outf.write("global {0}\n".format(defaultline))
            # second line must be a coordinate format
            outf.write("{0}\n".format(prev_cs))

            for shape in self:
                shape_attr = '' if prev_cs == shape.coord_format \
                    else shape.coord_format + "; "
                shape_excl = '-' if shape.exclude else ''
                text_coordlist = ["{:f}".format(f) for f in shape.coord_list]
                shape_coords = "(" + ",".join(text_coordlist) + ")"
                shape_comment = " # " + shape.comment if shape.comment else ''

                shape_str = (shape_attr + shape_excl + shape.name +
                             shape_coords + shape_comment)

                outf.write("{0}\n".format(shape_str))

        except IOError as e:
            cmsg = "Unable to create region file \'{:s}\'.".format(outfile)
            if e.args:
                e.args = (e.args[0] + '\n' + cmsg,) + e.args[1:]
            else:
                e.args = (cmsg,)
            raise e
        finally:
            if outf:
                outf.close()