user5661083
user5661083

Reputation: 107

Tkinter- UI objects to be serialized and saved in a new file

I want my entries to be saved in a file

filename= filedialog.asksaveasfile(mode="wb",defaultextension=".txt")
with open(filename, "wb") as f:
    dill.dump(data,f)

This code gives an error. Is there any other way of doing it.

TypeError: invalid file: <_io.BufferedWriter name='C:/Users/random.txt'>

Upvotes: 0

Views: 86

Answers (1)

Lafexlos
Lafexlos

Reputation: 7735

filedialog.asksaveasfile returns an opened file. Hence you are getting an error when you are trying open it again. Just try to use it as it was opened before.

file= filedialog.asksaveasfile(mode="wb",defaultextension=".txt")
if file is not None: # returns `None` if cancel's out
    dill.dump(data,file)

Upvotes: 2

Related Questions