Home > OS >  Select multiple directories and display the contents
Select multiple directories and display the contents

Time:09-23

The purpose of the code below is to select a directory. The issue with this code is that it doesn't display the content of the selected folder. example: Folder A has files B,C,D. I would like files of B,C,D to be displayed in the text box using tkinter.

from tkinter import *
from tkinter import filedialog

root = Tk()
root.geometry('200x200')
root.grid_rowconfigure(0, weight = 1)
root.grid_columnconfigure(0, weight = 1)

dirs = []
def get_directories():
    dirs.append(filedialog.askdirectory())
    return dirs

b1 = Button(root, text='select directories...', command = get_directories)
b1.pack()


root.mainloop()

I am all new to using tkinter and python programming. I hope I have managed to explain my problem.

CodePudding user response:

I have written an example for you. As you can see my code shows the files and directories separately in the selected folder (You can remove the directory part if it's not needed).

Code:

from tkinter import *
from tkinter import filedialog
from os import listdir
from os.path import isfile, isdir, join

root = Tk()
root.geometry("200x200")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)


def get_directories():
    selected_directory = filedialog.askdirectory()
    print("Selected directory: {}".format(selected_directory))

    content_of_selected_folder = listdir(selected_directory)

    print(content_of_selected_folder)

    for elem in content_of_selected_folder:
        if isfile(join(selected_directory, elem)):
            Label(root, text="File: {}".format(elem)).pack()
        if isdir(join(selected_directory, elem)):
            Label(root, text="Directory: {}".format(elem)).pack()


b1 = Button(root, text="select directories...", command=get_directories)
b1.pack()


root.mainloop()

Output of test:

>>> python3 test.py 
Selected directory: /home/user/work_dir/tools/venv3
['include', 'lib', 'lib64', 'bin', 'pyvenv.cfg', 'pip-selfcheck.json']

GUI:

GUI

If you want to show the complete path of elements:

for elem in content_of_selected_folder:
    full_path_of_element = join(selected_directory, elem)
    if isfile(full_path_of_element):
        Label(root, text="File: {}".format(full_path_of_element)).pack()
    if isdir(full_path_of_element):
        Label(root, text="Directory: {}".format(full_path_of_element)).pack()

GUI in case of full path:

GUI full paths

NOTE:

Based on your question title (I don't know if it's a mistake or true) you want to show content of multiple directories. It you really want to do that, then you can check the above snippet. If you click more times to select directories... button and select a folder then the list of content will be extended with the content of newly selected folder.

Related code part:

content_of_all_selected_dirs = []


def get_directories():
    selected_directory = filedialog.askdirectory()
    print("Selected directory: {}".format(selected_directory))

    content_of_all_selected_dirs.extend(listdir(selected_directory))

    print(content_of_all_selected_dirs)

    for elem in content_of_all_selected_dirs:
        full_path_of_element = join(selected_directory, elem)
        if isfile(full_path_of_element):
            Label(root, text="File: {}".format(full_path_of_element)).pack()
        if isdir(full_path_of_element):
            Label(root, text="Directory: {}".format(full_path_of_element)).pack()

GUI after selected /tmp/202108/20210817204008 directory:

GUI after one selection

GUI after selected /tmp/202109/20210909192402/resources directory:

GUI after two selection

  • Related