Python Forum

Full Version: Wrting to text file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

total python newbie here

A friend has been using F-engrave http://www.scorchworks.com/Fengrave/fengrave.html

they supply the source code and he would to change the code that write the g code to file. I have identified the code below.
how would I change this so it wrote all the lines in (self.gcode) > then write a line with text "g68 x0 y0 r180" > then repeat all the (self.gcode) lines > then write a line with text "g69" > then write a line with text "m30" > then write a line with text "%".

hope this makes sense

many thanks

def menu_File_Save_G_Code_File(self):
        if (self.Check_All_Variables() > 0):
            return

        if self.vcoords == [] and self.cut_type.get() == "v-carve":
            mess = "V-carve path data does not exist.  "
            mess = mess + "Only settings will be saved.\n\n"
            mess = mess + "To generate V-Carve path data Click on the"
            mess = mess + "\"Calculate V-Carve\" button on the main window."
            if not message_ask_ok_cancel("Continue", mess ):
                return

        self.WriteGCode()
        init_dir = os.path.dirname(self.NGC_FILE)
        if ( not os.path.isdir(init_dir) ):
            init_dir = self.HOME_DIR

        fileName, fileExtension = os.path.splitext(self.NGC_FILE)
        init_file=os.path.basename(fileName)




        if self.input_type.get() == "image":
            fileName, fileExtension = os.path.splitext(self.IMAGE_FILE)
            init_file=os.path.basename(fileName)
        else:
            init_file=""


        filename = asksaveasfilename(defaultextension='.ngc', \
                                    filetypes=[("G-Code File","*.ngc"),("TAP File","*.tap"),("All Files","*")],\
                                     initialdir=init_dir,\
                                    initialfile= init_file )

        if filename != '' and filename != ():
            self.NGC_FILE = filename
            try:
                fout = open(filename, 'w+' )
            except:
                self.statusMessage.set("Unable to open file for writing: %s" %(filename))
                self.statusbar.configure( bg = 'red' )
                return
            for line in self.gcode:
                print line
                try:
                    fout.write(line+'\n')
                    
                except:
                    fout.write('(skipping line)\n')
            fout.close()
     

            self.statusMessage.set("File Saved: %s" %(filename))
            self.statusbar.configure( bg = 'white' )
anyone please