Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask multiple directories dialog in Tkinter

I am trying to select multiple folders. I need the equivalent of askopenfilenames() for directories, but only askdirectory() exists, which only allows to select one folder.

Previously I found a custom script that did this for Matlab (uigetdir). Any way of doing this in Python?

I need to batch process files in about 50 folders at a time, selecting them one by one is not realistic.

Also, I'm not a programmer, just trying to process my geophysical data, wouldn't be able to "code it myself" as I've seen suggested in other places. Would have thought such a basic thing would be included in the basic functions.

like image 902
user4547612 Avatar asked Feb 09 '15 19:02

user4547612


1 Answers

Having had the same problem I developed my own solution. With this you can select one directory at a time then select cancel when you are finished.

The function the returns a list of the directories you have selected.

def fun_directory_selector(request_string: str, selected_directory_list: list, search_directory):
    directory_path_string = filedialog.askdirectory(initialdir=search_directory, title=request_string)

       if len(directory_path_string) > 0:
        selected_directory_list.append(directory_path_string)
        fun_directory_selector('Select the next Directory or Cancel to end', 
                               selected_directory_list,
                               os.path.dirname(directory_path_string))

    return selected_directory_list
like image 170
Roy Jaques Avatar answered Oct 15 '22 18:10

Roy Jaques