How to list all files in a listbox, subdirs included

You can now use the ListFile class, previously called FileList, to get a list of file with a certain extension in a listbox in tkinter. Now you can also, with the argumenti subdirs=1, have all the files, with absolute path too, also in the subdirectories, not also in the root folder.

Here is the code:

"""
How to make a GUI with Tkinter to open mp4 videos in a folder
""" from glob import glob import tkinter as tk import sys import os class ListFile: def __init__(self, filetype, diroot=".", subdirs=0): self.folder = os.path.abspath(diroot) self.filetype = filetype self.root = tk.Tk() self.root.geometry("400x800") self.lb = tk.Listbox(self.root) self.lb.configure(bg="gold") self.lb.pack(fill="both", expand=True) self.ListFile = glob("*." + self.filetype) if subdirs: self.find_all() else: self.find() self.root.mainloop() def find_all(self): # def searchfiles(extension='.txt', folder='H:\\'): for r, d, f in os.walk(self.folder): for file in f: if file.endswith(self.filetype): self.lb.insert(0, r + "\\" + file) def find(self): """ insert all files in the listbox """ for f in self.filelist: self.lb.insert(0,f) if __name__ == '__main__': ListFile("txt", subdirs=1)

Subscribe to the newsletter for updates
Tkinter templates
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts