Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 574
Patrick Dennis Avatar asked Nov 29 '25 16:11

Patrick Dennis


1 Answers

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)
like image 99
adder Avatar answered Dec 02 '25 05:12

adder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!