Extension not added to file name after saving
Asked Answered
D

2

6

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)
Derringdo answered 9/7, 2016 at 17:26 Comment(6)
Did you check the filenames from cmd.exe (dir) rather than Windows Explorer?Sulamith
Change the call to tkinter.filedialog.asksaveasfilename(Sulamith
@Sulamith I checked cmd.exe (dir) too, it also shows file with no extensionDerringdo
@Sulamith I changed the call to tkinter.filedialog.asksaveasfilename, it didn't solve my problem though. Actually it's normal because I have already imported tkinter.filedialog. It would completely not work, if it was used incorrectly.Derringdo
Your blanket except/pass logic is a bad idea, you have no idea if the code errorsGasoline
@PadraicCunningham I have used exception handling to prevent FileNotFoundError if user cancels saving.Derringdo
D
14

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! :)

Derringdo answered 10/7, 2016 at 0:15 Comment(8)
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! ;)Derringdo
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.Ontogeny
@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. ;)Derringdo
@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.Derringdo
I seriously would not really care about downvoting my reputation here, if it did not limit my priveleges to ask my questions.Derringdo
@BryanOakley thanks for helping me understand the rules!Derringdo
"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. stackoverflow.com/help/how-to-ask and meta.#262092 might be useful.Ontogeny
Sadly, your comment about people just down voting other - is more than corrent.Sobriety
J
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
Johnsen answered 9/7, 2016 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.