I was wondering if there is something similar to Java's JFileChooser for Python?
JFileChooser is a graphical front end to choose a file.
Preferably something that is already with Python. Maybe with Tkinter.
I was wondering if there is something similar to Java's JFileChooser for Python?
JFileChooser is a graphical front end to choose a file.
Preferably something that is already with Python. Maybe with Tkinter.
Easiest way I ever found to do this (using PyGTK and Kiwi):
from kiwi.ui.dialogs import open as open_dialog
chosen_path = open_dialog('Select a file', folder='/start/folder')
if chosen_path is not None:
# do something ...
For something that doesn't require wxPython and sticks with the standard Python libs, you can use the tkFileDialog.askopenfilename() method:
#!/usr/bin/python
from Tkinter import *
from tkFileDialog import askopenfilename
root = Tk()
root.withdraw()
print askopenfilename()
For python 3 what you're looking for is tkinter.filedialog, and all that comes with it. Here's a short program that opens and then prints a TXT file of the user's choosing via askopenfilename:
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw()
root.update()
pathString = askopenfilename(filetypes=[("Text files","*.txt")])
if pathString:
openFile = open(pathString, 'r')
fileString = openFile.read()
print(fileString)
root.destroy()
Output is whatever is in the selected file.
pathString (not for empty string): if pathString: (see this answer stackoverflow.com/a/47067803/1908115 )