How Python normally handles file objects should be something you read into, to get a general idea of it.
with acts as context manager and handles the open and close automatically, further reading:
https://docs.python.org/3/library/stdtypes.html#typecontextmanagerCode
data = 'heh'
with open('file.txt', 'w') as f:
f.write(data)
or, another normal Python method for opening and closing, which is normally not used because
with is better at handling opening and closing streams
Code
data = 'heh'
f = open('file.txt', 'w')
f.write(data)
f.close()
Tkinter has its own methods setup for it though, should work similarly. Here is something explaining in more detail the file handling aspect and methods:
http://tkinter.unpythonic.net/wiki/tkFileDialogasksaveasfile(mode='w', **options)Code
import Tkinter, Tkconstants, tkFileDialog
class TkFileDialogExample(Tkinter.Frame):
def __init__(self, root):
Tkinter.Frame.__init__(self, root)
self.file_opt = options = {}
def asksaveasfile(self):
"""Returns an opened file in write mode."""
return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)
If you want to browse the source of this, I found
https://github.com/python/cpython/blob/master/Lib/tkinter/filedialog.py which seems to show all the details of how it is organized, you will notice it is a fancy rewrite of Python's
open() with some functional fluff.
