Adicione uma barra de progresso. [code]
- User
Adicione uma barra de progresso.
pythonimport os import math import zipfile def create_zip_archives(base_dir, zip_size_limit_gb=40, output_dir='/home/paperspace/karaoke-version-dataset-zip'): # Garante que o diretório de saída exista os.makedirs(output_dir, exist_ok=True) # Set the size limit per ZIP file (in bytes) zip_size_limit = zip_size_limit_gb * 1024 * 1024 * 1024 # Convert GB to bytes if output_dir is None: output_dir = base_dir # Use base directory if output directory not specified files_info = [] # Collect all files and their sizes for root, dirs, files in os.walk(base_dir): for file in files: full_path = os.path.join(root, file) if os.path.isfile(full_path): # Ensure it's a file size = os.path.getsize(full_path) files_info.append({'path': full_path, 'size': size}) total_size = sum(f['size'] for f in files_info) num_bins = math.ceil(total_size / zip_size_limit) # Warn the user if the number of ZIP files exceeds 4 if num_bins > 4: print(f"Warning: Total dataset size is {total_size/(10243):.2f} GB, which requires {num_bins} ZIP files to stay under {zip_size_limit_gb} GB per ZIP file.") # Initialize bins for files bins = [{'files': [], 'size': 0} for _ in range(num_bins)] # Sort files by size in descending order files_info.sort(key=lambda x: x['size'], reverse=True) # Distribute files into bins for f in files_info: placed = False # Try to place the file into a bin without exceeding the size limit for b in bins: if b['size'] + f['size'] <= zip_size_limit: b['files'].append(f['path']) b['size'] += f['size'] placed = True break if not placed: # If the file doesn't fit in any bin, create a new bin new_bin = {'files': [f['path']], 'size': f['size']} bins.append(new_bin) num_bins += 1 print(f"Added an extra ZIP file to accommodate file {f['path']}.") # Create ZIP files for idx, b in enumerate(bins): zip_filename = os.path.join(output_dir, f"karaoke_archive_part{idx+1}.zip") print(f"Creating {zip_filename} with {len(b['files'])} files, total size: {b['size']/(10243):.2f} GB.") with zipfile.ZipFile(zip_filename, 'w', compression=zipfile.ZIP_STORED, allowZip64=True) as zf: for file_path in b['files']: # Add file to ZIP with a relative path arcname = os.path.relpath(file_path, start=base_dir) zf.write(file_path, arcname=arcname) print("ZIP file creation complete.") # Example usage: if __name__ == "__main__": base_directory = '/home/paperspace/karaoke-version-dataset' create_zip_archives(base_directory) - Assistant