<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def __scripts(self, filestem):
 """ Generates the required scripts. """

        script_construct = open('%s/%s' % (self.tmpdir, RSD.CONSTRUCT), 'w')
        script_save = open('%s/%s' % (self.tmpdir, RSD.SAVE), 'w')
        script_subgroups = open('%s/%s' % (self.tmpdir, RSD.SUBGROUPS), 'w')

        # Permit the owner to execute and read this script
        for fn in RSD.SCRIPTS:
            os.chmod('%s/%s' % (self.tmpdir, fn), S_IREAD | S_IEXEC)

        # Writes one line of script
        new_script = lambda script: lambda x: script.write(x + '\n')

        #
        # 'Construction' script
        #
        w = new_script(script_construct)
        w(':- initialization(main).')
        w('main :-')
        w('[featurize],')
        w('r(%s),' % filestem)
        w('w.')
        script_construct.close()

        #
        # 'Saving' script
        #
        w = new_script(script_save)
        w(':- initialization(main).')
        w('main :-')
        w('[process],')
        w('r(%s),' % filestem)
        w('w,')
        w('w(weka, %s),' % filestem)
        w('w(rsd, %s).' % filestem)
        script_save.close()

        #
        # 'Subgroups' script
        #
        w = new_script(script_subgroups)
        w(':- initialization(main).')
        w('main :-')
        w('[rules],')
        w('r(%s),' % filestem)
        w('i,')
        w('w.')
        script_subgroups.close()