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)
Share:
342,323

Related videos on Youtube

Mustafa Zengin
Author by

Mustafa Zengin

Updated on October 01, 2021

Comments

  • Mustafa Zengin
    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
      Priya almost 3 years
      Good question. I was just looking for this. I upvoted it. Thanks!
  • user391339
    user391339 about 10 years
    I got TypeError: 'module' object is not callable on Tk().withdraw() - any ideas?
  • user391339
    user391339 about 10 years
    I had to do root = Tk.Tk() then root.withdraw(). Now the open file dialog window does not close however.
  • imallett
    imallett about 7 years
    For total parallelism, should probably also have import tkinter + tkinter.Tk().withdraw().
  • Yonatan Naor
    Yonatan Naor almost 7 years
    This is the best solution so far. The main reason is that easygui is a pip package and easy to install
  • WestAce
    WestAce almost 6 years
    Using Python 3.x and I believe "Tkinter" is actually supposed to be all lowercase, "tkinter".
  • Ben
    Ben almost 6 years
    @WestAce yes, it was changed from "Tkinter" to "tkinter" for Python3
  • Ben Vincent
    Ben Vincent over 5 years
    this 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
    Cabara over 5 years
    same here. the file dialog won't close
  • eric
    eric over 4 years
    this code is the exact same as the accepted answer but incomplete.
  • Christopher Barber
    Christopher Barber over 4 years
    On 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
    Bricktop over 4 years
    Why am I getting invalid syntax error for print easygui.diropenbox()?
  • jfs
    jfs over 4 years
  • gaya
    gaya over 4 years
    @ChristopherBarber same with 10.14.6. Python just keeps quitting.
  • gaya
    gaya over 4 years
    On Mac 10.14.6, this opened the File finder then it just crashed the entire system :(
  • miguelmorin
    miguelmorin over 4 years
    It is not part of standard installation in Python 3.
  • Shantanu Shinde
    Shantanu Shinde over 4 years
    Is there any way to allow only certain types of files? for eg. I want the user to select image files only
  • Craig
    Craig over 4 years
    This appears to be for Python 2 as easygui attempts to import Tkinter instead of tkinter
  • Rafael Ruales
    Rafael Ruales over 3 years
    @ShantanuShinde I think this may work: filename = askopenfilename(filetypes=[("Image files", "*.png"), ("All Files", "*.*")])
  • CalfCrusher
    CalfCrusher about 3 years
    it makes python 3.9 crash on OSX 10.11.6
  • Rich Lysakowski PhD
    Rich Lysakowski PhD about 3 years
    This code crashes Jupyter Notebook v6.1.5 on Windows 10.
  • Priya
    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
    Marc over 2 years
    Py3 code doesn't work. See @Sainath Reddy at the bottom here.
  • HelpMeCode
    HelpMeCode almost 2 years
    How would I then read this file path into a pd.read_excel() @StefanoPalazzo?
  • HelpMeCode
    HelpMeCode almost 2 years
    How 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