Here is a simple function to show a file chooser right in the terminal window.
This method supports selecting multiple files or directories. This has the added benefit of running even in an environment where GUI is not supported.
from os.path import join,isdir
from pathlib import Path
from enquiries import choose,confirm
def dir_chooser(c_dir=getcwd(),selected_dirs=None,multiple=True) :
'''
This function shows a file chooser to select single or
multiple directories.
'''
selected_dirs = selected_dirs if selected_dirs else set([])
dirs = { item for item in listdir(c_dir) if isdir(join(c_dir, item)) }
dirs = { item for item in dirs if join(c_dir,item) not in selected_dirs and item[0] != "." } # Remove item[0] != "." if you want to show hidde
options = [ "Select This directory" ]
options.extend(dirs)
options.append("⬅")
info = f"You have selected : \n {','.join(selected_dirs)} \n" if len(selected_dirs) > 0 else "\n"
choise = choose(f"{info}You are in {c_dir}", options)
if choise == options[0] :
selected_dirs.add(c_dir)
if multiple and confirm("Do you want to select more folders?") :
return get_folders(Path(c_dir).parent,selected_dirs,multiple)
return selected_dirs
if choise == options[-1] :
return get_folders(Path(c_dir).parent,selected_dirs,multiple)
return get_folders(join(c_dir,choise),selected_dirs,multiple)
To install enquiers do,
pip install enquiries