Choosing a file in Python with simple Dialog
342,323
Solution 1
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!
Solution 2
Python 3.x version of Etaoin's answer for completeness:
from tkinter.filedialog import askopenfilename
filename = askopenfilename()
Solution 3
With EasyGui:
import easygui
print(easygui.fileopenbox())
To install:
pip install easygui
Demo:
import easygui
easygui.egdemo()
Solution 4
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()
Solution 5
This worked for me
Reference : https://www.youtube.com/watch?v=H71ts4XxWYU
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)
Related videos on Youtube
Author by
Mustafa Zengin
Updated on October 01, 2021Comments
-
Mustafa Zengin over 2 years
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?
-
Priya almost 3 yearsGood question. I was just looking for this. I upvoted it. Thanks!
-
-
user391339 about 10 yearsI got TypeError: 'module' object is not callable on Tk().withdraw() - any ideas?
-
user391339 about 10 yearsI had to do root = Tk.Tk() then root.withdraw(). Now the open file dialog window does not close however.
-
imallett about 7 yearsFor total parallelism, should probably also have
import tkinter
+tkinter.Tk().withdraw()
. -
Yonatan Naor almost 7 yearsThis is the best solution so far. The main reason is that easygui is a pip package and easy to install
-
WestAce almost 6 yearsUsing Python 3.x and I believe "Tkinter" is actually supposed to be all lowercase, "tkinter".
-
Ben almost 6 years@WestAce yes, it was changed from "Tkinter" to "tkinter" for Python3
-
Ben Vincent over 5 yearsthis does not work for me (on Mac, Python 3.6.6) The GUI window opens but you cannot close it and you get beachball of death
-
Cabara over 5 yearssame here. the file dialog won't close
-
eric over 4 yearsthis code is the exact same as the accepted answer but incomplete.
-
Christopher Barber over 4 yearsOn Mac OSX 10.14.5, python 3.6.7, easygui 0.98.1 I get a horrible crash when I try this. Not recommended.
-
Bricktop over 4 yearsWhy am I getting
invalid syntax
error forprint easygui.diropenbox()
? -
jfs over 4 years@Bricktop stackoverflow.com/questions/826948/… ?
-
gaya over 4 years@ChristopherBarber same with 10.14.6. Python just keeps quitting.
-
gaya over 4 yearsOn Mac 10.14.6, this opened the File finder then it just crashed the entire system :(
-
miguelmorin over 4 yearsIt is not part of standard installation in Python 3.
-
Shantanu Shinde over 4 yearsIs there any way to allow only certain types of files? for eg. I want the user to select image files only
-
Craig over 4 yearsThis appears to be for Python 2 as easygui attempts to import Tkinter instead of tkinter
-
Rafael Ruales over 3 years@ShantanuShinde I think this may work: filename = askopenfilename(filetypes=[("Image files", "*.png"), ("All Files", "*.*")])
-
CalfCrusher about 3 yearsit makes python 3.9 crash on OSX 10.11.6
-
Rich Lysakowski PhD about 3 yearsThis code crashes Jupyter Notebook v6.1.5 on Windows 10.
-
Priya almost 3 years@Etaoin Awesome. This work perfect for me. I did not wanted to do a whole GUI just for selecting a file purpose
-
Marc over 2 yearsPy3 code doesn't work. See @Sainath Reddy at the bottom here.
-
HelpMeCode almost 2 yearsHow would I then read this file path into a
pd.read_excel()
@StefanoPalazzo? -
HelpMeCode almost 2 yearsHow would I then take this file path and put it into a pandas data frame (if the file was, say, from excel) pd.read_excel(r'file_path') does not work @SainathReddy