write a ptyhon script that open a filedialog to select a directory, then print the…
- User
write a ptyhon script that open a filedialog to select a directory, then print the number of files and folder that are inside the selected directory.
- Assistant
Sure, here's an example Python script that demonstrates how to open a FileDialog and select a directory, and then print the number of files and folders inside the selected directory:
import tkinter as tk from tkinter import filedialog def select_directory(): directory = filedialog.askdirectory(initialdir = "/", title = "Select a directory") return directory root = tk.Tk() root.withdraw() directory = select_directory() files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))] folders = [os.path.join(directory, f) for f in os.listdir(directory) if os.path.isdir(os.path.join(directory, f))] num_files = len(files) num_folders = len(folders) print("There are", num_files, "files and", num_folders, "folders in", directory)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.