Question

I am making a python program to pickle some data and then save it to a file. It isn't working.

path="C:/foo/bar"
def newData():
    import pickle
    data = [
        ["foo"],
        ["bar"],
        ["baz"]
    ]
    file = None
    i = 0

    while file==None:
        if not exists(path + str(i)+".data"):
            file = open(path + str(i)+".data", "w+")
        else:
            i+=1
    pickle.dump(data, file)
    close(file)
    return path + str(i)+".data"

I get a name error saying that "global name close is not defined":

  File "C:/foo/bar/baz.py", line 26, in newData
    close(file)
NameError: global name 'close' is not defined

If I comment the line with close in it, I get:

  File "C:/foo/bar/baz.py", line 26, in newData
    #close(file)
NameError: global name 'close' is not defined

Is my code wrong (yes)? how?

Was it helpful?

Solution

You are editing your file, but your web server is still running the old code. The traceback reads the source file to show you the line, but that doesn't mean it's running that line. This is clear from the traceback you mentioned that indicates a comment line as the problem.

As @JBernardo commented, you need to change "close(file)" to "file.close()", and then you need to force your web server to reload the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top