def commit(self):
        """ Close the open temp stream and replace the original file,
            optionally making a backup copy.
        """
        assert self.stream, "Write stream not open"

        # close temp file
        self.__close()

        # do optional backup and rename temp file to the correct name
        if self.do_backup:
            replaceFile(self.filename, self.basename + ".bak")
        replaceFile(self.__tempfile(), self.filename)
 Close the open temp stream and replace the original file,
            optionally making a backup copy.
        
recipes/Python/56037_Safely_morph_a_file_inplace/recipe-56037.py