# Define the input and output file paths
input_file_path = "old.gcode"
output_file_path = "new.gcode"

# Open the input and output files
with open(input_file_path, "r") as input_file, open(output_file_path, "w") as output_file:
    # Loop through each line in the input file
    for line in input_file:
        # Check if the current line starts with "G0" or "G1"
        if line.startswith("G0"):
            # Add "M107" to a new line above the current line
            output_file.write("M107\n")
        elif line.startswith("G1"):
            # Add "M106" to a new line above the current line
            output_file.write("M106\n")

        # Write the current line to the output file
        output_file.write(line)

# Close the files
input_file.close()
output_file.close()

