In this video I’ll show you how to find the number of files in any directory using Tkinter and Python.

We’ll use the os libary that comes with Python, specifically os.walk()

This will return the files and directories in any given directory. We’ll loop thru and count the files, then output that to a Tkinter Label!

Python Code: checker.py
(Github Code)

from tkinter import *
from tkinter import filedialog
import os


root = Tk()
root.title("Check Number Of Files...")
root.iconbitmap('c:/tkinter.com/codemy.ico')
root.geometry('500x350')

def checker():
	# Create counters
	file_count = 0
	dir_count = 0

	# Choose Directory
	input_path = filedialog.askdirectory()

	# Loop and count files
	for root, dirs, files in os.walk(input_path):
		# Count files
		for file in files:
			file_count += 1

		# Count Dirs
		for dir1 in dirs:
			dir_count +=1

		# Update our Label
		my_label.config(text=f'Number Of Files: {file_count}\nNumber of Dirs: {dir_count}')
		




my_label = Label(root, text="Number of Files: ", font=("Helvetica", 28))
my_label.pack(pady=50)

my_button = Button(root, text='Check Number Of Files', command=checker)
my_button.pack()



root.mainloop()



John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...