import gzip import shutil import os def gzip_file(source_file, delete_original=False): """ Compress any file using gzip. Args: source_file (str): Full path to the file to compress. delete_original (bool): Whether to delete the original file after compression. Returns: str: Path to the gzipped file. """ if not os.path.isfile(source_file): raise FileNotFoundError(f"File not found: {source_file}") gzipped_file = source_file + '.gz' with open(source_file, 'rb') as f_in: with gzip.open(gzipped_file, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) if delete_original: os.remove(source_file) print(f"Original file deleted: {source_file}") print(f"Gzipped file created: {gzipped_file}") return gzipped_file