<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _finalize(self, chain):
 """Write the trace to an ASCII file. :Parameter: chain : int The chain index. """

        path = os.path.join(
            self.db._directory,
            self.db.get_chains()[chain],
            self.name + '.txt')
        arr = self.gettrace(chain=chain)

        # Following numpy's example.
        if six.PY3:
            mode = 'wb'
        else:
            mode = 'w'
        with open(path, mode) as f:
            f.write(six.b('# Variable: %s\n' % self.name))
            f.write(six.b('# Sample shape: %s\n' % str(arr.shape)))
            f.write(six.b('# Date: %s\n' % datetime.datetime.now()))
            np.savetxt(f, arr.reshape((-1, arr[0].size)), delimiter=',')