Skip to main content

chat1

from pathlib import enumPath

classdef MenuOptions(enum.Enum)list_and_select_files(directory, prefixes):
    OPTION_1 = "First Option"""
    OPTION_2Lists =files "Secondin Option"a specified directory filtered by prefixes, allows the user to select files,
    OPTION_3and =returns "Thirdthe Option"paths of the selected files. The files are sorted by modification time,
    EXITwith =the "Exitmost Program"

recently

#modified Creatingfiles aappearing first.
    
    Parameters:
    directory (str): The path to the directory containing the files.
    prefixes (list of str): A list of prefixes to filter the MenuOptionsfiles.
    
    Returns:
    list of Path: Paths of the files selected by the user.
    """
    dir_path = Path(directory)
    # Filter and sort files
    files = [file for easyfile indexingin dir_path.iterdir() if file.is_file() and any(file.name.startswith(prefix) for prefix in prefixes)]
options_list =   list(MenuOptions)files.sort(key=lambda x: x.stat().st_mtime, reverse=True)

def display_menu(   # Display files
    if not files:
        print("No files found with the specified prefixes."):
        return []
    print("\nPleasenSelect selectfiles oneby orentering moretheir optionsnumbers (separateseparated choicesby with commas):commas:")
    for index, optionfile in enumerate(options_list,files, start=1):
        mod_time = file.stat().st_mtime
        formatted_time = Path(file).stat().st_mtime
        print(f"{index}. {option.value}file.name} (modified: {formatted_time})")

def get_user_choices():
    input_str
    # User selection
    selection = input("\nEnterEnter your choices by numberselection (e.g., 1,2)3,5): ")
    choice_numbers = input_str.split(',')
    choicesselected_files = []
    try:
        for number in choice_numbers:
            indexindices = [int(number.num.strip()) - 1 for num in selection.split(',')]  # Convert input to zero-based index
        ifselected_files = [files[index] for index <in 0 or index >= len(options_list):
                print(f"Number out of range. Please enter a number between 1 and {len(options_list)}.")
            else:
                choices.append(options_list[index])indices]
    except ValueError:(IndexError, ValueError):
        print("Invalid input. Please enter valid numbers.numbers corresponding to the file list.")

    return choices

def process_choices(choices):
    for choice in choices:
        if choice is MenuOptions.EXIT:
            print("Exiting...")
            return False
        else:
            print(f"You selected: {choice.value}")
    return True

def main():
    while True:
        display_menu()
        choices = get_user_choices()
        if not process_choices(choices):
            breakselected_files

if __name__ == "__main__":
    main(directory = input("Enter the directory path: ")
    prefixes_input = input("Enter file prefixes, separated by commas (e.g., log, report, 2021): ")
    prefixes = [prefix.strip() for prefix in prefixes_input.split(',')]
    selected_files = list_and_select_files(directory, prefixes)
    print("You selected the following files:")
    for file in selected_files:
        print(file.name)