Get a file name with tkinter.filedialog.asksaveasfilename to append in it
<p>from an GUI application designed with <code>tkinter</code>, I wish to save some datas in a file in appending mode. To get the file's name I use <code>asksaveasfilename</code> from <code>filedialog</code> module. Here is the code:</p>

<pre><code>from tkinter.filedialog import asksaveasfilename

def save_file():

    file_name = asksaveasfilename()

    if file_name:
        f = open(file_name, 'a')
        contents = tab_chrono.text_area.get(1.0, 'end')
        f.write(contents)
        f.close()
</code></pre>

<p>The problem happens when I select in the dialog an existing file, I got a warning that the file will be overwritten. It is not true since I append in the file. 
Is there a way to get rid of this warning ? Or do I have to rewrite a <code>askappendfilename</code> myself ? This is missing in <code>filedialog</code> module.
<a href="https://i.stack.imgur.com/mPL5s.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/mPL5s.jpg" alt="enter image description here"></a></p>

<p>Use the option <code>confirmoverwrite</code> to prevent the message, when selecting an existing file.</p>

<pre><code>import tkFileDialog 
import time
class Example():

    dlg = tkFileDialog.asksaveasfilename(confirmoverwrite=False)
    fname = dlg
    if fname != '':
        try:
            f = open(fname, "rw+")
            text = f.read()
            print text
        except:
            f = open(fname, "w")
        new_text = time.time()
        f.write(str(new_text)+'\n')
        f.close()      
</code></pre>

<p>Edit: Note that I am using <code>f.read()</code> to be able to print the existing text.<br>
You may want to remove the <code>f.read()</code> and subsequent <code>print</code> statement and replace them with a <code>f.seek(0,2)</code> which positions the pointer at the end of the existing file.<br>
The other option is as follows using the <code>append</code> option in the file open, which will create the file if it doesn't already exist:</p>

<pre><code>import tkFileDialog 
import time
class Example():

    dlg = tkFileDialog.asksaveasfilename(confirmoverwrite=False)
    fname = dlg
    if fname != '':
        f = open(fname, "a")
        new_text = time.time()
        f.write(str(new_text)+'\n')
        f.close()      
</code></pre>

python tkinter filedialog