Untitled
import os import zipfile import shutil import sys def extract_and_copy(zip_file, destination_path): # Check if the zip file exists if not os.path.isfile(zip_file): print(f"Error: The file '{zip_file}' does not exist.") return # Check if the destination path exists if not os.path.exists(destination_path): print(f"Error: The destination path '{destination_path}' does not exist.") return # Create a temporary directory to extract files temp_dir = "temp_unzip_dir" os.makedirs(temp_dir, exist_ok=True) try: # Extract the ZIP file into the temporary directory with zipfile.ZipFile(zip_file, 'r') as zip_ref: zip_ref.extractall(temp_dir) print(f"Extracted files to temporary directory: {temp_dir}") # Copy files from the temporary directory to the destination path for root, _, files in os.walk(temp_dir): for file in files: source_path = os.path.join(root, file) destination_file_path = os.path.join(destination_path, file) shutil.copy2(source_path, destination_file_path) print(f"Copied {file} to {destination_path}") except zipfile.BadZipFile: print(f"Error: '{zip_file}' is not a valid ZIP file.") finally: # Clean up the temporary directory shutil.rmtree(temp_dir) print("Temporary directory cleaned up.") if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python script.py <path_to_zip> <destination_path>") else: zip_file = sys.argv[1] destination_path = sys.argv[2] extract_and_copy(zip_file, destination_path)
Leave a Comment