4

I get files with no extension after saving them, although I give them extensions by filetypes option in my program. I can only do it using defaultextension option, but I want to let user decide to choose an extension without messing with code. Plus, when I use defaultextension option, for example: defaultextension=".txt", it adds 2 .txt to my file name, like filename.txt.txt. Here's my snippet:

from tkinter import *
import tkinter.filedialog

root = Tk()
root.title("Saving a File")
root.geometry("500x500-500+50")

def save():
    filename = filedialog.asksaveasfilename(
        initialdir="D:",
        title="Choose your file",
        filetypes=(
            ("Text Files", "*.txt"),
            ("Python Files", "*.py"),
            ("All Files", "*.*")
            )
        )

    try:
        fileobj = open(filename, 'w')
        fileobj.write(text.get(0.0, "end-1c"))
        fileobj.close()
    except:
        pass

button = Button(root, text="Save", command=save,
                     cursor='hand2', width=30, height=5,
                     bg='black', fg='yellow', font='Helvetica')
button.pack()

text = Text(root)
text.pack()

I do not have any problem with writing a file, my problem is only with their extensions.

Extra info:

  • I'm on Windows 7
  • I've unchecked Hide extensions for known file types (I've tried checked version but it didn't change anything)
halfer
  • 19,824
  • 17
  • 99
  • 186
Parviz Karimli
  • 1,247
  • 1
  • 14
  • 26

2 Answers2

12

Great! I myself found the answer just by adding defaultextension="*.*" option. Thanks for everyone for trying to answer my question, although none of them solved my problem, in fact, most of them only downvoted my question without explaining their reasons. Well, it was not my fault if you don't know a solution LOL! Thanks for others who tried to help me! Appreciated! :)

Parviz Karimli
  • 1,247
  • 1
  • 14
  • 26
  • It even raises a warning message: `The file name is not valid.` when you don't choose a proper file extension from the list. Works pretty well, just like I desired! ;) – Parviz Karimli Jul 10 '16 at 00:21
  • One of the reasons for downvotes is when the question shows no signs of research, or of trying to solve the problem yourself. Your original question had that problem. – Bryan Oakley Jul 10 '16 at 03:18
  • @BryanOakley If I am posting a question here, that means I have looked for a solution before asking here (I'm not dumb). Answerers should provide sources to reference to. This time, I truly looked for a solution in many popular sources like effbot, tcl/tk doc etc, even SO, but I didn't find a solution to my answer. Is this what you want to hear? LOL OK, next time, I will add those sources to my question. ;) – Parviz Karimli Jul 10 '16 at 09:57
  • @BryanOakley No, my intention was not to answer my own question. I honestly didn't know how to solve it. I asked but nobody's answer solved my problem. Meanwhile, I had been trying to solve it on my own too, and finally, got the solution! So I decided to post my answer. I didn't know this could violate the rules. – Parviz Karimli Jul 10 '16 at 09:58
  • I seriously would not really care about downvoting my reputation here, if it did not limit my priveleges to ask my questions. – Parviz Karimli Jul 10 '16 at 09:59
  • @BryanOakley thanks for helping me understand the rules! – Parviz Karimli Jul 10 '16 at 10:02
  • _"If I am posting a question here, that means I have looked for a solution before asking here (I'm not dumb)"_ -- that's not an assumption we can make. Many, many people ask questions without reading documentation or searching for similar questions. All we have to go on is what you write. Your question needs to be thorough, and show research and an attempt to solve the problem. http://stackoverflow.com/help/how-to-ask and http://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users might be useful. – Bryan Oakley Jul 10 '16 at 14:38
  • Sadly, your comment about people just down voting other - is more than corrent. – guyd Nov 28 '17 at 07:49
0

idlelib.IOBinding (.iomenu in 3.6) has this code that works to add .py or .txt when not explicitly entered. I don't know/remember what "TEXT" is for, but since the code works, I leave it alone.

    filetypes = [
        ("Python files", "*.py *.pyw", "TEXT"),
        ("Text files", "*.txt", "TEXT"),
        ("All files", "*"),
        ]
    [...]
    def asksavefile(self):
        dir, base = self.defaultfilename("save")
        if not self.savedialog:
            self.savedialog = tkFileDialog.SaveAs(
                    parent=self.text,
                    filetypes=self.filetypes,
                    defaultextension=self.defaultextension)
        filename = self.savedialog.show(initialdir=dir, initialfile=base)
        return filename
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52