The Python Oracle

Choosing a file in Python with simple Dialog

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle

--

Chapters
00:00 Question
00:32 Accepted answer (Score 277)
00:52 Answer 2 (Score 106)
01:07 Answer 3 (Score 45)
01:26 Answer 4 (Score 13)
01:47 Thank you

--

Full question
https://stackoverflow.com/questions/3579...

Answer 2 links:
[EasyGui]: https://easygui.readthedocs.io/en/master/

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #userinterface #dialog #filechooser

#avk47



ACCEPTED ANSWER

Score 290


How about using tkinter?

from Tkinter import Tk     # from tkinter import Tk for Python 3.x
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!




ANSWER 2

Score 112


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

from tkinter.filedialog import askopenfilename
filename = askopenfilename()



ANSWER 3

Score 46


With EasyGui:

import easygui
print(easygui.fileopenbox())

To install:

pip install easygui

Demo:

import easygui
easygui.egdemo()



ANSWER 4

Score 14


In Python 2 use the tkFileDialog module.

import tkFileDialog

tkFileDialog.askopenfilename()

In Python 3 use the tkinter.filedialog module.

import tkinter.filedialog

tkinter.filedialog.askopenfilename()