Browse for file path in python
I think TkFileDialog might be useful for you.
import Tkinterimport tkFileDialogimport osroot = Tkinter.Tk()root.withdraw() #use to hide tkinter windowcurrdir = os.getcwd()tempdir = tkFileDialog.askdirectory(parent=root, initialdir=currdir, title='Please select a directory')if len(tempdir) > 0: print "You chose %s" % tempdir
EDIT: this link has some more examples
This will generate a GUI with just a button called 'Browse', which prints out the file path that you choose from the browser. The type of the file can be specified by changing the code segment <*.type>.
from Tkinter import * import tkFileDialogimport sysif sys.version_info[0] < 3: import Tkinter as Tkelse: import tkinter as Tkdef browse_file():fname = tkFileDialog.askopenfilename(filetypes = (("Template files", "*.type"), ("All files", "*")))print fnameroot = Tk.Tk()root.wm_title("Browser")broButton = Tk.Button(master = root, text = 'Browse', width = 6, command=browse_file)broButton.pack(side=Tk.LEFT, padx = 2, pady=2)Tk.mainloop()
I remade Roberto's code, but rewritten in Python3 (just minor changes).
You can copy-and-paste as is for an easy demonstration .py file, or just copy the function "search_for_file_path" (and associated imports) and place into your program as a function.
import tkinterfrom tkinter import filedialogimport osroot = tkinter.Tk()root.withdraw() #use to hide tkinter windowdef search_for_file_path (): currdir = os.getcwd() tempdir = filedialog.askdirectory(parent=root, initialdir=currdir, title='Please select a directory') if len(tempdir) > 0: print ("You chose: %s" % tempdir) return tempdirfile_path_variable = search_for_file_path()print ("\nfile_path_variable = ", file_path_variable)