diff --git a/simple_file_zipper/README.md b/simple_file_zipper/README.md new file mode 100644 index 0000000..347d6f6 --- /dev/null +++ b/simple_file_zipper/README.md @@ -0,0 +1,36 @@ +# Simple File Zipper + +This is a command-line utility that gathers all `.txt` files in its directory and compresses them into a new `.zip` archive. + +## Description + +This script demonstrates the power of Python's built-in `zipfile` module. It's a practical tool for basic file backup and compression. + +When run, the script will: +1. Create two dummy `.txt` files (`file1_to_zip.txt`, `file2_to_zip.txt`) for demonstration. +2. Ask the user for a name for the output zip file. +3. Use the `glob` module to find all files ending in `.txt`. +4. Create a new `.zip` archive and add all found text files to it. + +## Features + +* **File Compression:** Creates a standard `.zip` archive. +* **Pattern Matching:** Uses `glob` to find all `.txt` files. +* **Practical Utility:** A useful script for simple backups. +* **Error Handling:** Includes `try...except` blocks for file I/O operations. + +## How to Run + +1. Ensure you have Python 3 installed. +2. Run the script from your terminal: + ```sh + python simple_zipper.py + ``` +3. Follow the prompt to name your zip file. +4. A new `.zip` archive will appear in the directory. + +## Modules Used + +* **`zipfile`**: (Python's built-in module for reading/writing zip archives) +* **`os`**: (Built-in module for creating test files) +* **`glob`**: (Built-in module for finding files that match a pattern) \ No newline at end of file diff --git a/simple_file_zipper/simple_zipper.py b/simple_file_zipper/simple_zipper.py new file mode 100644 index 0000000..c0f46d9 --- /dev/null +++ b/simple_file_zipper/simple_zipper.py @@ -0,0 +1,49 @@ +import os +import zipfile +import glob + +print("--- Simple File Zipper ---") +print("This script will find all .txt files and add them to a new .zip archive.") + +# --- 1. Create dummy files to zip --- +# This makes the script easy to test immediately. +try: + with open("file1_to_zip.txt", "w") as f: + f.write("This is the first test file.") + with open("file2_to_zip.txt", "w") as f: + f.write("This is the second test file.") + print("Created 'file1_to_zip.txt' and 'file2_to_zip.txt' for testing.") +except IOError as e: + print(f"Error creating test files: {e}") + exit() # Exit if we can't even write files + +# --- 2. Get the desired archive name --- +zip_name = input("Enter the name for your new zip file (e.g., 'archive.zip'): ") +if not zip_name.endswith(".zip"): + zip_name += ".zip" # Ensure it has the .zip extension + +# --- 3. Find all .txt files in the current directory --- +# glob.glob is a simple way to find files matching a pattern +txt_files = glob.glob("*.txt") + +if not txt_files: + print("No .txt files were found in this directory to zip.") +else: + print(f"\nFound {len(txt_files)} .txt file(s):") + for f in txt_files: + print(f" - {f}") + + # --- 4. Create the zip file and add files --- + try: + # 'w' mode means write a new zip file (will overwrite if it exists) + with zipfile.ZipFile(zip_name, 'w') as zf: + for file_path in txt_files: + # Add the file to the zip archive + zf.write(file_path) + # Optional: Remove the original file after zipping + # os.remove(file_path) + + print(f"\nSuccessfully created '{zip_name}' containing {len(txt_files)} file(s).") + + except Exception as e: + print(f"\nAn error occurred while creating the zip file: {e}") \ No newline at end of file