Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browse for file path in python

I am trying to create a GUI with a browse window to locate a specific file. I found this question earlier: Browsing file or directory Dialog in Python

although when I looked up the terms it didn't seem to be what I was looking for.

All I need is something launchable from a Tkinter button that returns a the path of the selected file from the browser.

Anybody have a resources for this?

EDIT: Alright so the question has been answered. To anybody with a similar question, do your research, the code out there DOES work. DO NOT test it in cygwin. it doesn't work in there for some reason.

like image 894
Funkyguy Avatar asked Nov 13 '13 03:11

Funkyguy


3 Answers

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 tkinter
from tkinter import filedialog
import os

root = tkinter.Tk()
root.withdraw() #use to hide tkinter window

def 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 tempdir


file_path_variable = search_for_file_path()
print ("\nfile_path_variable = ", file_path_variable)
like image 97
Shane Rooney Avatar answered Nov 16 '22 20:11

Shane Rooney


I think TkFileDialog might be useful for you.

import Tkinter
import tkFileDialog
import os

root = Tkinter.Tk()
root.withdraw() #use to hide tkinter window

currdir = 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

like image 20
Roberto Avatar answered Nov 16 '22 22:11

Roberto


In python 3 it has been renamed to filedialog. you can access a folder pass by askdirectory method(event) as follows. If you want to choose a file path use askopenfilename

import tkinter 
from tkinter import messagebox
from tkinter import filedialog

main_win = tkinter.Tk()
main_win.geometry("1000x500")
main_win.sourceFolder = ''
main_win.sourceFile = ''
def chooseDir():
    main_win.sourceFolder =  filedialog.askdirectory(parent=main_win, initialdir= "/", title='Please select a directory')

b_chooseDir = tkinter.Button(main_win, text = "Chose Folder", width = 20, height = 3, command = chooseDir)
b_chooseDir.place(x = 50,y = 50)
b_chooseDir.width = 100


def chooseFile():
    main_win.sourceFile = filedialog.askopenfilename(parent=main_win, initialdir= "/", title='Please select a directory')

b_chooseFile = tkinter.Button(main_win, text = "Chose File", width = 20, height = 3, command = chooseFile)
b_chooseFile.place(x = 250,y = 50)
b_chooseFile.width = 100

main_win.mainloop()
print(main_win.sourceFolder)
print(main_win.sourceFile )

Note: the value of variables persist even after closing the main_win. However, you need to use the variable as an attribute of the main_win i.e.

main_win.sourceFolder
like image 5
Afshin Amiri Avatar answered Nov 16 '22 21:11

Afshin Amiri