You can simply use the writelink (type _io.TextIOWrapper) function returned by the function asksaveasfile.
for instance
from tkinter import filedialog, Tk
root = Tk().withdraw()
file = filedialog.asksaveasfile(mode='w', defaultextension=".csv")
if file:
file.write("Hello World")
file.close()
Note that the object returned by the function asksaveasfilehas the same type or class of the object that is returned by the built-in function open. Also note that the same function returns Noneif you click when a dialog box appears Cancel.
source
share