0

I'm trying to save the answers from a GUI that has radio buttons that has different value ranging from A, B and C, but when I'm trying my code it completely rewrite the first letters and not going to the next line.

    self.pushButton.clicked.connect(lambda: 
    self.btnA_clk(self.radioButton_16.isChecked()))
    self.pushButton.clicked.connect(lambda: 
    self.btnB_clk(self.radioButton_17.isChecked()))
    self.pushButton.clicked.connect(lambda: 
    self.btnC_clk(self.radioButton_18.isChecked()))



def btnA_clk(self, clkA):
    if clkA:
        textfile = open("studentexam.txt", "w")
        print("A")
        textfile.write("A")
        textfile.close()

def btnB_clk(self, clkB):
    if clkB:
        textfile = open("studentexam.txt", "w")
        print("B")
        textfile.write("B")
        textfile.close()

def btnC_clk(self, clkC):
    if clkC:
        textfile = open("studentexam.txt", "w")
        print("C")
        textfile.write("C")
        textfile.close()

My output in my console is a b c d e but in the text file it has only 1 letter written in the first line which is the last letter.

  • 1
    `open("studentexam.txt", "a")`. Use mode `"a"` to append. Read more here: https://docs.python.org/3/library/functions.html?highlight=open#open mode `"w"` truncates and overwrite your file. – r.ook Jan 22 '19 at 15:01
  • i need the letters to be save like a\n b\n c\n d\n e\n – Jefer Bulan Jan 22 '19 at 15:03
  • `textfile.write("A\n")`? – r.ook Jan 22 '19 at 15:04
  • Do you need to open and close your file for each function call separately? Maybe better solution would be opening file once at start and closing at end? – Daweo Jan 22 '19 at 15:06
  • i'd try "a" sir but mode a continuously adding letters, i need to make my text file blank after the rerun. Is it possible sir? – Jefer Bulan Jan 22 '19 at 15:07
  • the syntax textfile.write("A\n") is working but it continuously adding letters and not clearing the previous letters when I rerun it. – Jefer Bulan Jan 22 '19 at 15:10
  • Then you will need to decide where is the checkpoint to truncate and overwrite the file. If by "rerun" you mean the next time you execute the program, you could consider having a function to clear the file upon opening. You can't expect the program to know what you want to do by just using one mode, you have to explicitly tell it so. As it stands however your code can benefit from the [DRY principle](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). – r.ook Jan 22 '19 at 15:10
  • what approach or functions I could use to make my file became blank again after opening ? – Jefer Bulan Jan 22 '19 at 15:16

2 Answers2

3

Every time your code does this:

textfile = open("studentexam.txt", "w")

you are opening the file afresh and overwriting what was there before. The same as File | Save does in an application, except that your code doesn't ask "Are you sure?". Use mode "a" instead of "w".

From the documentation for open():

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending.

r.ook
  • 13,466
  • 2
  • 22
  • 39
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • what approach or functions I could use to make my file became blank again after opening ? because it continuously writing letters. I need to make it that in every end of the run it will save. but if the user run it again it will became blank so it doesn't continue – Jefer Bulan Jan 22 '19 at 15:19
  • One way: Provide another function attached to a button called *Clear file* that opens the file in mode `"w"` and immediately closes it again without writing anything. – BoarGules Jan 22 '19 at 15:22
  • can i attach the function that i will make into the submit button or i will make a different button for the clear files sir ? – Jefer Bulan Jan 22 '19 at 15:26
  • Up to you. I don't really know exactly what your program is supposed to do. That is why I said in my comment that is was *one way* to do it. Your question was about *how* to clear the file: you know that now. But only you can say *when* you want that to happen. – BoarGules Jan 22 '19 at 15:29
  • sir last question how can I make 1 function that can clear and write in the file? and how can I implement it in the button ? sorry sir I have many questions because I am new in python – Jefer Bulan Jan 22 '19 at 15:35
  • 1
    Ask a new question, including your program with the fixes so far, say what is happening and what you want to happen. Comments are not handy for this. – BoarGules Jan 22 '19 at 15:38
0
# param = 'clkA'
def button_click(self, param):
    with open('studentexam.txt', 'a') as f:
        print(param[-1])
        f.write(param[-1])

Confused by python file mode "w+"

ps. try to avoid duplication of the code when each function does same functionality ... if possible of course.

naivepredictor
  • 898
  • 4
  • 14