Patrick Dennis
Patrick Dennis

Reputation: 331

tkinter.filedialog open file and savefile methods . How to choose?

tkinter.filedialog ...

asksaveasfile() vs asksaveasfilename()

askopenfile() vs askopenfilename()

when would I use one vs the other?

Upvotes: 3

Views: 2418

Answers (1)

adder
adder

Reputation: 3698

asksaveasfilename() and askopenfilename() return only the path to the selected file as a str object. On the other hand, askopenfile() and asksaveasfile() return an actual file-like object (technically, an object of type _io.TextIOWrapper) which you can use to read from or write to later on. For instance:

with tkinter.filedialog.askopenfile() as f:
    contents = f.read()
    print(contents)

Upvotes: 4

Related Questions