Use the askopenfilename() function to display an open file dialog that allows users to select one file. Use the askopenfilenames() function to display an open file dialog that allows users to select multiple files.
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!
Python 3.x version of Etaoin's answer for completeness:
from tkinter.filedialog import askopenfilename
filename = askopenfilename()
With EasyGui:
import easygui
print(easygui.fileopenbox())
To install:
pip install easygui
Demo:
import easygui
easygui.egdemo()
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()
Another option to consider is Zenity: http://freecode.com/projects/zenity.
I had a situation where I was developing a Python server application (no GUI component) and hence didn't want to introduce a dependency on any python GUI toolkits, but I wanted some of my debug scripts to be parameterized by input files and wanted to visually prompt the user for a file if they didn't specify one on the command line. Zenity was a perfect fit. To achieve this, invoke "zenity --file-selection" using the subprocess module and capture the stdout. Of course this solution isn't Python-specific.
Zenity supports multiple platforms and happened to already be installed on our dev servers so it facilitated our debugging/development without introducing an unwanted dependency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With