Python writing and reading a txt file
<pre><code>def replace():
    import tkinter.filedialog
    drawfilename = tkinter.filedialog.askopenfilename()
    list1= int(open(drawfilename,'w'))
    del list1[-3:]

    input_list = input("Enter three numbers separated by commas: ")
    list2 = input_list.split(',')
    list2 = [int(x.strip())for x in list2]


    list1[0:0] = list2
    list1.write(list1)
    list1.close()

    import tkinter.filedialog
    drawfilename = tkinter.filedialog.askopenfilename()
    list1= open(drawfilename,'r')
    line = list1.readlines()
    list1.close()
</code></pre>

<p>I want to open a <code>.txt</code> file containing <code>1,2,3,4,5,6,7,8,9</code>,  remove the last three values then ask a user to input three numbers and add them to the beginning of the list ( example input <code>12,13,14</code> gives <code>12,13,14, 1,2,3,4,5,6</code>). Then I want to overwrite the original list with this new list. When a user opens the routine again, I want list1 to be the new list1.
With the help of stackflow I get the new list1, but am having difficulty opening and rewriting to the text file. The error that global list1 has not been declared stops the routine from progressing.</p>
