The Python Oracle

Choosing a file in Python with simple Dialog

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop

--

Chapters
00:00 Choosing A File In Python With Simple Dialog
00:22 Answer 1 Score 14
00:38 Accepted Answer Score 290
00:55 Answer 3 Score 46
01:09 Answer 4 Score 112
01:19 Thank you

--

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

--

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()