
Below is a Python script that accomplishes the task as described in title. It lists all folders in a given directory, prompts the user to choose one, and defaults to the latest folder if no choice is made.
It lists all folders in a given directory, prompts the user to choose one, and defaults to the latest folder (topmost) if no choice is made. The chosen folder’s contents are then displayed:
import os
import time
def list_folders(directory):
"""
Lists all folders in the specified directory.
"""
folders = []
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
if os.path.isdir(item_path):
folders.append(item_path)
return folders
def get_user_choice(folders, timeout=10):
"""
Asks the user to choose a folder from the list.
Defaults to the latest folder if no choice is made within the timeout.
"""
print("Available folders (latest modified first):")
folders.sort(key=lambda f: os.path.getmtime(f), reverse=True)
for i, folder in enumerate(folders):
print(f"{i + 1}. {folder}")
start_time = time.time()
while True:
user_input = input("Enter the number of the folder you want (or press Enter for the latest folder): ")
if user_input:
try:
choice_index = int(user_input) - 1
chosen_folder = folders[choice_index]
break
except (ValueError, IndexError):
print("Invalid choice. Please try again.")
elif time.time() - start_time > timeout:
print("No choice made. Using the latest folder.")
chosen_folder = folders[0]
break
return chosen_folder
def display_folder_contents(folder):
"""
Displays the contents of the chosen folder.
"""
print(f"Contents of folder '{folder}':")
for item in os.listdir(folder):
print(item)
def main():
target_directory = "/path/to/your/directory" # Replace with the actual directory path
folders_list = list_folders(target_directory)
if not folders_list:
print("No folders found in the specified directory.")
return
chosen_folder = get_user_choice(folders_list)
display_folder_contents(chosen_folder)
if __name__ == "__main__":
main()
Make sure to replace /path/to/your/directory with the actual path to the directory you want to work with. The script will list content of chosen_folder directory. Feel free to customize the script further as needed! 🐍📂

Below is updated the script to include a timeout for user input. If the user doesn’t choose among the given values within a specified time, it will default to the latest folder (topmost). The chosen folder’s contents will be displayed in reverse chronological order, along with its size and the number of items in the folder:. Here’s the modified Python script:
import os
import time
def list_folders(directory):
"""
Lists all folders in the specified directory.
"""
folders = []
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
if os.path.isdir(item_path):
folders.append(item_path)
return folders
def get_user_choice(folders, timeout=10):
"""
Asks the user to choose a folder from the list.
Defaults to the latest folder if no choice is made within the timeout.
"""
print("Available folders (latest modified first):")
folders.sort(key=lambda f: os.path.getmtime(f), reverse=True)
for i, folder in enumerate(folders):
print(f"{i + 1}. {folder}")
start_time = time.time()
while True:
user_input = input("Enter the number of the folder you want (or press Enter for the latest folder): ")
if user_input:
try:
choice_index = int(user_input) - 1
chosen_folder = folders[choice_index]
break
except (ValueError, IndexError):
print("Invalid choice. Please try again.")
elif time.time() - start_time > timeout:
print("No choice made. Using the latest folder.")
chosen_folder = folders[0]
break
return chosen_folder
def display_folder_contents(folder):
"""
Displays the contents of the chosen folder in reverse chronological order.
"""
print(f"Contents of folder '{folder}':")
folder_items = os.listdir(folder)
folder_items.sort(key=lambda f: os.path.getmtime(os.path.join(folder, f)), reverse=True)
for item in folder_items:
item_path = os.path.join(folder, item)
print(f"{item} (Modified: {time.ctime(os.path.getmtime(item_path))})")
folder_size = sum(os.path.getsize(os.path.join(folder, f)) for f in folder_items)
num_items = len(folder_items)
print(f"Folder size: {folder_size} bytes")
print(f"Number of items: {num_items}")
def main():
target_directory = "/path/to/your/directory" # Replace with the actual directory path
folders_list = list_folders(target_directory)
if not folders_list:
print("No folders found in the specified directory.")
return
chosen_folder = get_user_choice(folders_list)
display_folder_contents(chosen_folder)
if __name__ == "__main__":
main()
Remember to replace /path/to/your/directory with the actual path to your desired directory. Now the script will wait for user input for up to 10 seconds, and if no choice is made, it will default to the latest folder. 🕒📂

Dr. Amit is a seasoned IT leader with over two decades of international IT experience. He is a published researcher in Generative AI and chatbot architectures (Springer & IJAET), with a PhD in Generative AI focused on human-like intelligent systems.
Amit believes there is vast potential for authentic expression within the tech industry. He enjoys sharing knowledge and coding, with interests spanning cutting-edge technologies, leadership, Agile Project Management etc. He previously earned top honors in his MCA.




