0

heres the error I'm getting. I know I probably shouldn't be using globals. I call th functions in the order init, and then adddata:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    adddata("testing", "1, 2, 3")
  File "/home/runner/EasyJSON/func.py", line 38, in adddata
    if(firsttime == True):
UnboundLocalError: local variable 'firsttime' referenced before assignment

heres the code thats returning faulty:

def init(*filename):
    global firsttime
    firsttime = True
    global file
    file = ""
    global undesirablechars
    undesirablechars = ["(", ")", "'", ","]
    for element in filename:
        if element == undesirablechars:
            pass
        else:
            file = element

def adddata(key, value):
    keyinfile = ""
    valueinfile = ""
    for element in key:
        if element == undesirablechars:
            pass
        else:
            keyinfile += element
    for element in value:
        if element == undesirablechars:
            pass
        else:
            valueinfile += element
        with open(file, 'w') as writefile:
            data = {keyinfile: valueinfile}
            if(firsttime == True):
                json.dump(data, writefile)
                firsttime = False
            else:
                adddatatofile = json.load(writefile)
                adddatatofile.update(data)
                writefile.seek(0)
                json.dump(adddatatofile, writefile)

This is on Python 3.8.7. It's something I'm working on to make using json easier.

Kronifer
  • 25
  • 4

0 Answers0