from PIL import Image import os import shutil import zipfile import readline # Get the current directory current_directory = os.getcwd() # Define image file extensions image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'] # Define ext as a global variable ext = tuple(image_extensions) directory_history = [] def list_images(): # List image files in the current working directory print("Image files in the current working directory:") for item in os.listdir(os.getcwd()): if os.path.isfile(item) and any(item.lower().endswith(ext) for ext in image_extensions): print(f"- {item}") def option1(): list_images() # Check if any images were found, and return to the menu if not images = [filename for filename in os.listdir(current_directory) if os.path.isfile(os.path.join(current_directory, filename)) and filename.lower().endswith(ext)] if not images: print("No image files found.") return # Ask the user for the image file name (assuming it's in the current directory) image_name = input("Enter the name of the image file (in the same directory): ") # Check if the file exists if not image_name in images: print("File not found.") else: # Ask the user for the new size try: width = int(input("Enter the new width: ")) height = int(input("Enter the new height: ")) # Open the image file image = Image.open(os.path.join(current_directory, image_name)) # Ask the user for the new file name new_image_name = input("Enter the name for the resized image file (with extension): ") # Resize the image resized_image = image.resize((width, height)) # Save the resized image with the user-specified name resized_image.save(os.path.join(current_directory, new_image_name)) print(f"Image resized and saved as {new_image_name}.") except ValueError: print("Invalid input. Please enter valid dimensions.") def option2(): list_images() # Check if any images were found, and return to the menu if not images = [filename for filename in os.listdir(current_directory) if os.path.isfile(os.path.join(current_directory, filename)) and filename.lower().endswith(ext)] if not images: print("No image files found.") return # Ask the user for the image file name (assuming it's in the current directory) image_name = input("Enter the name of the image file (in the same directory): ") # Check if the file exists if not image_name in images: print("File not found.") else: # Open the image file image = Image.open(os.path.join(current_directory, image_name)) width, height = image.size print(f"Image size: {width}x{height} pixels.") def list_directories(): # List directories in the current working directory print("Directories in the current working directory:") for item in os.listdir(os.getcwd()): if os.path.isdir(item): print(f"- {item}") def list_directories_and_files(): # List directories and individual files in the current working directory print("Directories and files in the current working directory:") for item in os.listdir(os.getcwd()): if os.path.isdir(item) or os.path.isfile(item): print(f"- {item}") def option3(): # Option 3: Zip specified directories and individual files list_directories_and_files() # List directories and individual files before asking for user input directories_and_files_to_zip = input("Enter the directories/files to zip (comma-separated): ").split(',') master_folder_name = "master_folder" if not os.path.exists(master_folder_name): os.mkdir(master_folder_name) source_directory = os.getcwd() target_directory = os.path.join(source_directory, master_folder_name) for item_name in directories_and_files_to_zip: item_name = item_name.strip() # Remove leading/trailing whitespace item_path = os.path.join(source_directory, item_name) if os.path.exists(item_path): if os.path.isfile(item_path): # Zip individual files shutil.copy(item_path, target_directory) elif os.path.isdir(item_path): # Zip directories target_dir_path = os.path.join(target_directory, item_name) shutil.copytree(item_path, target_dir_path) zip_filename = "master_folder.zip" with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, _, files in os.walk(master_folder_name): for file in files: file_path = os.path.join(root, file) relative_path = os.path.relpath(file_path, master_folder_name) zipf.write(file_path, arcname=relative_path) shutil.rmtree(master_folder_name) print("Option 3: Process completed successfully.") def list_zip_files(): # List all .zip files in the current working directory zip_files = [file for file in os.listdir(current_directory) if file.endswith('.zip')] if zip_files: print("Zip files in the current working directory:") for zip_file in zip_files: print(f"- {zip_file}") else: print("No .zip files found in the current working directory.") return False # No .zip files found, return False return True def option4(): if not list_zip_files(): return # No .zip files found, return to the main menu zip_files_to_extract = input("Enter the zip file(s) to extract (comma-separated): ").split(',') for zip_file_name in zip_files_to_extract: zip_file_name = zip_file_name.strip() # Remove leading/trailing whitespace zip_file_path = os.path.join(current_directory, zip_file_name) if not os.path.exists(zip_file_path): print(f"Zip file '{zip_file_name}' not found.") else: with zipfile.ZipFile(zip_file_path, 'r') as zipf: zipf.extractall(current_directory) print(f"Successfully extracted '{zip_file_name}'.") def option5(): # Get the directory where the script was initially run script_directory = os.getcwd() # List all files and directories in the script directory print(f"Files and directories in the directory '{script_directory}':") items_in_directory = os.listdir(script_directory) for index, item_name in enumerate(items_in_directory): print(f"{index + 1}. {item_name}") # Ask the user to enter the name of the item to delete item_name_to_delete = input("Enter the name of the item you want to delete: ") item_path_to_delete = os.path.join(script_directory, item_name_to_delete) # Check if the item exists if os.path.exists(item_path_to_delete): try: if os.path.isfile(item_path_to_delete): os.remove(item_path_to_delete) print(f"File '{item_name_to_delete}' has been deleted.") elif os.path.isdir(item_path_to_delete): os.rmdir(item_path_to_delete) print(f"Directory '{item_name_to_delete}' has been deleted.") except Exception as e: print(f"An error occurred: {e}") else: print(f"'{item_name_to_delete}' does not exist in the directory.") def option6(): # List the contents of the current directory list_directories_and_files() # Ask the user for the files or directories to move items_to_move = input("Enter the files/directories to move (comma-separated): ").split(',') # Ask the user for the destination path destination_path = input("Enter the destination path: ") # Check if the destination path exists if not os.path.exists(destination_path): print("Destination path does not exist.") return for item_name in items_to_move: item_name = item_name.strip() # Remove leading/trailing whitespace item_path = os.path.join(current_directory, item_name) if os.path.exists(item_path): destination_item_path = os.path.join(destination_path, item_name) try: if os.path.isfile(item_path): # Move individual files shutil.move(item_path, destination_item_path) elif os.path.isdir(item_path): # Move directories shutil.move(item_path, destination_item_path) print(f"Moved '{item_name}' to '{destination_path}'.") except Exception as e: print(f"An error occurred while moving '{item_name}': {e}") else: print(f"'{item_name}' does not exist in the current directory.") def ls(): # Get the directory where the script was initially run script_directory = os.getcwd() # List all files and directories in the script directory print(f"Files and directories in the directory '{script_directory}':") items_in_directory = os.listdir(script_directory) for index, item_name in enumerate(items_in_directory): print(f"{index + 1}. {item_name}") return def change_directory(path): global current_directory # Check if the specified directory exists if os.path.exists(path) and os.path.isdir(path): directory_history.append(current_directory) current_directory = path os.chdir(current_directory) print(f"Changed directory to '{path}'.") else: print(f"Directory '{path}' does not exist.") def go_back(): global current_directory if directory_history: current_directory = directory_history.pop() os.chdir(current_directory) print(f"Changed directory back to '{current_directory}'.") else: print("No directory history to go back.") def read_input_with_history(prompt): try: # Use readline to enable history support line = input(prompt) readline.add_history(line) return line except EOFError: # Handle Ctrl+D gracefully return None while True: print("Terminal Options:") print("ls - List the contents in the directory") print("cd - Type cd /thedirectory/)") print("back - Go back to the previous directory") print("x - Quit") print("Image Handling:") print("1 - Resize an image") print("2 - Get image size") print("File Handling: ") print("3 - Copy specified directories and create a zip file") print("4 - Unzip specified zip files") print("5 - Delete a file in the currently selected directory") print("6 - Move files to a specified path") choice = read_input_with_history("Enter your choice (ls/cd/back/1/2/3/4/5/6/x): ") if choice == 'ls': ls() elif choice.startswith('cd '): path = choice[3:] # Extract the directory path change_directory(path) elif choice == 'back': go_back() elif choice == '1': option1() elif choice == '2': option2() elif choice == '3': option3() elif choice == '4': option4() elif choice == '5': option5() elif choice == '6': option6() elif choice == 'x': break else: print("Invalid choice. Please enter ls, cd, back, 1, 2, 3, 4, 5, or x.")