-3

TL;DR
When I run my program it deletes all my txt file data, I do not understand why.

I'm making a waypoint editor/saver for my robot arm, and this program is meant to allow me to choose from a 'set' which is just a txt file, and then change or set a new point in that 'set' and it works to an extent. when I run the program I can enter a point, change values and save them, and if I choose to set another point my old point is still there, and if I end the program I can check the txt file and it has all the values. the problem is when I rerun the program, even if I don't get past the first input command, and I stop it immediately it will clear all my text files, and I haven't even chosen witch txt file to edit, I understand that it will run all imported modules when I run the program, but none of those subprograms were giving me grief in my other version of my robot arm control system so I am at a loss as to what I'm doing wrong, I've been at this problem for 2 days now and haven't gotten anywhere. I apologize for my total lack of commenting, I'm trying to work on that but I was excited to make this program and got carried away.

    from machine import Pin, PWM, ADC, I2C
    from time import sleep
    import Move
    from pico_i2c_lcd import I2cLcd

    def EDITMODE(): 
     analog_value = machine.ADC(28)
     i2c = I2C(0, sda=Pin(0), scl =Pin(1), freq=400000)
     I2C_ADDR = i2c.scan()[0]
     lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
     stv=0
     base = PWM(Pin(18))
     base.freq(50)
     lcdt = 1
     j2 = PWM(Pin(19))
     j2.freq(50)
     j3 = PWM(Pin(20))
     j3.freq(50)
     claw = PWM(Pin(21))
     claw.freq(50)
     buttonmove = 20000
     b1 = Pin(2, Pin.IN, Pin.PULL_DOWN)
     b2 = Pin(3, Pin.IN, Pin.PULL_DOWN)
     b3= Pin(4, Pin.IN, Pin.PULL_DOWN)
     b4= Pin(5, Pin.IN, Pin.PULL_DOWN)
     b5= Pin(6, Pin.IN, Pin.PULL_DOWN)
     b6= Pin(7, Pin.IN, Pin.PULL_DOWN)
     b7= Pin(8, Pin.IN, Pin.PULL_DOWN)
     b8= Pin(9, Pin.IN, Pin.PULL_DOWN)
     b9= Pin(10, Pin.IN, Pin.PULL_DOWN)
     b10= Pin(11, Pin.IN, Pin.PULL_DOWN)
     b11= Pin(12, Pin.IN, Pin.PULL_DOWN)
     b12= Pin(13, Pin.IN, Pin.PULL_DOWN)
     bshift= Pin(14, Pin.IN, Pin.PULL_DOWN)
     l1led = Pin(16, Pin.OUT)
     l2led= Pin(17, Pin.OUT)
     global gha
     gha = 0
     buttonmove = 5000
     j1v =1222500
     j4v =1282500
     j2v =1312500
     j3v=1762500
     break1 = 0

     while True:
        if break1 ==1:
            break
        #decides which waypoint to edit
        eprog = int(input('Which Set would you like to edit (1-5) '))
        if eprog == 1:
            curentprogram = 'Prog1.txt'
        elif eprog == 2:
            curentprogram = 'Prog2.txt'
        elif eprog == 3:
            curentprogram = 'Prog3.txt'
        elif eprog == 4:
            curentprogram = 'Prog4.txt'
        elif eprog == 5:
            curentprogram = 'Prog5.txt'
        file = open(curentprogram, "r")
        WPL = file.readlines()
        file.close()
        x = len(WPL)
        print(WPL)
        print('There are ', str(x/6), ' Waypoints currently set')
        epoint1 =  int(input('Which waypoint would you like to set? '))
        epoint = (epoint1*6)
        if epoint >0:
            epoint = epoint+1
        print('Move robot to desired position then save waypoint')
        sleepvalue = 0
        while True:
            b1v = b1.value()
            b2v = b2.value()
            b3v = b3.value()
            b4v = b4.value()
            b5v = b5.value()
            b6v = b6.value()
            b7v = b12.value()
            b8v = b11.value()
            b9v = b10.value()
            b10v = b9.value()
            b11v = b8.value()
            b12v = b7.value()
            shiftb = bshift.value()
            if shiftb == 1:
                l2led(0)
                if b1v ==1:
                    j1v = j1v+buttonmove
                if b12v ==1:
                    j1v = j1v-buttonmove
                if b2v ==1:
                    j2v = j2v+buttonmove
                if b11v ==1:
                    j2v = j2v-buttonmove
                if b3v ==1:
                    j4v = j4v - buttonmove
                if b10v ==1:
                    j4v = j4v + buttonmove
                if b4v ==1:
                    j3v = j3v+buttonmove
                if b9v ==1:
                    j3v = j3v-buttonmove
            else:
                l2led(1)
                if b6v == 1:
                    sleepvalue = sleepvalue+1
                    print('sleep time =', sleepvalue)
                    sleep(.5)
                if b7v == 1:
                    sleepvalue = sleepvalue-1
                    if sleepvalue < 0:
                        sleepvalue = 0
                    print('sleep time =', sleepvalue)
                    sleep(.5)
                if b1v == 1:
                    print(epoint, x)
                    if epoint+.01 > (x/6):
                        print('value not written, new waypoint value = ', str((x/6)+1))
                        WPL.append(j1v)
                        WPL.append(j2v)
                        WPL.append(j3v)
                        WPL.append(j4v)
                        WPL.append(x/6)
                        WPL.append(sleepvalue)
                    else:
                        print('Saving Point')
                        print(epoint)
                        epoint = int(epoint)
                        WPL[epoint] = j1v
                        WPL[epoint+1] = j2v
                        WPL[epoint+2] = j3v
                        WPL[epoint+3] = j4v
                        WPL[epoint+4] = (epoint/6)
                        WPL[epoint+5] = sleepvalue
                        print(WPL)
                    file  =  open(curentprogram, 'w')
                    file.write('')
                    file.close
                    file = open(curentprogram, 'a')
                    print(WPL)
                    for item in WPL:
                        print(item, 'bannana')
                        file.write((str(item))+'\n')
                    file.close()
                    restart = input('would you like to set another point? (y/n) ')
                    if restart == 'n' or restart == 'N':
                        return
                    else:
                        break
            sleep(.025)
            print(j1v, j2v, j3v, j4v)
            #base.duty_ns(int(j1v))
            #j2.duty_ns(int(j2v))
            #j3.duty_ns(int(j3v))
            #claw.duty_ns(int(j4v))

    if __name__ == '__main__':
        print('hello')
        EDITMODE()
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • you write empty string in your `curentprogram` – Ashkan Goleh Pour Jan 02 '23 at 09:05
  • Do the files disappear or get emptied? – bert Jan 02 '23 at 09:06
  • yes, I write the empty string so I can rewrite the edited list back to the txt file, and the files just get emptied. – Atticus Atwood Jan 02 '23 at 09:09
  • This is because that time, you are opening the file with `w` rather than `a`. You must use `a` to append data to a file. – bert Jan 02 '23 at 09:24
  • 2
    As a side note: you should really learn to use lists instead of all these b1, b2... – Thierry Lathuille Jan 02 '23 at 09:31
  • I only use 'w' to clear the file first and then I use 'a' to write back the edited data in a for loop, if I'm doing this wrong please correct me, but I don't see the problem, and the issue I'm having happens before this in the program if I stop the program as soon as I run it still deletes all the txt in the file, even though it never reaches that section in the code – Atticus Atwood Jan 02 '23 at 09:35
  • and i did not know i could use lists to assign pin values to variables, I will look into that – Atticus Atwood Jan 02 '23 at 09:36

1 Answers1

0

The issue comes from the following line:

file  =  open(curentprogram, 'w')

When appending data to a file, you must use mode a for append. Otherwise, when using w, if a file with that name exists, it is removed.

According to the documentation:
[...] mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end.

EDIT
Since I cannot run your code, I would suggest you to start with a simpler script and start building up yours from there. For instance, lets some.txt be a text file with he following content:

$ cat some.txt
foo

Then you can experiment with the following code:

with open('some.txt', 'r') as file:
    lines = file.readlines()

print(lines)
# this prints: ['foo\n']

with open('some.txt', 'w') as file:
    file.write('')

with open('some.txt', 'a') as file:
    file.write('bar\n')

then:

$ cat some.txt
bar

So, this logic seems to work fine. From here, try to see what you are doing and how it might affect the i/o operations.

bert
  • 372
  • 4
  • 13
  • so when I read the file and assign it to the list I use 'r' and when I go back to write the data I use 'w' to first clear the file then 'a' to add the edited values back to the list. I believe I'm doing this correctly as the documentation states, but if not, please correct me. – Atticus Atwood Jan 02 '23 at 09:32
  • also, I belive the problem is earlier in the code, as I can run the beginning of the code and stop it immediately and the values in the txt file are still deleted. – Atticus Atwood Jan 02 '23 at 09:38
  • Please, read the edit and try to start from there. That might be the best way to fix this without being able to run the code. – bert Jan 02 '23 at 09:43
  • Or is it possible that WPL get emptied in the last iteration for some reason? – bert Jan 02 '23 at 09:46
  • I will definitely look over my code more, and check to see if I'm doing my txt editing wrong, but I don't think that is the problem, because when I run the code and stop it immediately when it hits the input('enter set to edit') it will still delete my data, it never even gets close to editing or reading or appending the file, I haven't even defined wich file to edit, it just deletes them all. i may be severely wrong and I appreciate your help but I don't see how it could be changing the text file whatsoever if I hadn't even defined wich file im editing. – Atticus Atwood Jan 02 '23 at 09:51
  • i don't think that WPL gets emptied because I have it print out what it is saving to the file and that works just fine, it saves to the file, but then when I rerun it it deletes the data – Atticus Atwood Jan 02 '23 at 09:53
  • Are you sure the files have any content at all? – bert Jan 02 '23 at 09:59
  • when, because when i am done with the program, the waypoint that I saved is there, but when I run the program again it gets deleted and if I go through the program again it will save the new point, but the old point is not there, it gets deleted at the very beginning for some reason – Atticus Atwood Jan 02 '23 at 10:02