3

from an GUI application designed with tkinter, I wish to save some datas in a file in appending mode. To get the file's name I use asksaveasfilename from filedialog module. Here is the 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()

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 askappendfilename myself ? This is missing in filedialog module. enter image description here

Jal
  • 205
  • 1
  • 2
  • 8
  • if you always select file to append (but you newer create new one) then you could use `askopenfilename` but it will display button "Open" instead of 'Save` (you can set window title="Open to append") . Or you will have to create own dialog. – furas Oct 13 '16 at 08:54
  • Unfortunately I want to create a new file if the file doesn't exist. – Jal Oct 13 '16 at 10:19
  • So you will have to create own Dialog. Try `print(tkinter.filedialog.__file__)` to see path to file with `asksaveasfilename` (and other file dialogs). You may use this code to create own version. Or create from scratch :) – furas Oct 13 '16 at 10:26

2 Answers2

5

The asksaveasfilename dialog accepts a confirmoverwrite argument to enable or disable the file existence check.

file_name = asksaveasfilename(confirmoverwrite=False)

This can be found in the Tk manual for tk_getSaveFile but doesn't appear to be documented for tkinter. It was introduced in Tk 8.5.11 so is relatively new in Tk terms (released Nov 2011).

patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • Also documented at the following links, yet none of them are Tkinter. https://www.tcl.tk/man/tcl/TkCmd/getOpenFile.htm http://tcl.tk/cgi-bin/tct/tip/382.html http://www.manpagez.com/man/n/tk_getSaveFile/ – Rolf of Saxony Oct 14 '16 at 14:24
3

Use the option confirmoverwrite to prevent the message, when selecting an existing file.

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()      

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

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()      
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • If the file doesn't exists, askopenfile generates an error message. If the file doesn't exist I want to create it. If it exists, I want to append into it – Jal Oct 13 '16 at 10:17
  • You didn't stipulate that in your question. See my edited answer. – Rolf of Saxony Oct 13 '16 at 14:45