Choosing a file in Python with simple Dialog


Question

I would like to get file path as input in my Python console application.

Currently I can only ask for full path as an input in the console.

Is there a way to trigger a simple user interface where users can select file instead of typing the full path?

1
109
9/12/2017 11:27:35 PM

Accepted Answer

How about using tkinter?

from Tkinter import Tk
from tkinter.filedialog import askopenfilename

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)

Done!

181
6/15/2018 8:45:33 PM

Python 3.x version of Etaoin's answer for completeness:

from tkinter.filedialog import askopenfilename
filename = askopenfilename()

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon