Skip to content

Instantly share code, notes, and snippets.

@RoryQ
Created February 28, 2025 01:03
Show Gist options
  • Select an option

  • Save RoryQ/ba27f6ede7e11a36d1f40d0e0dee439b to your computer and use it in GitHub Desktop.

Select an option

Save RoryQ/ba27f6ede7e11a36d1f40d0e0dee439b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import argparse
from pathlib import Path
def duplicate_file(input_file: str, output_file: str, target_size: str) -> None:
# Convert size string (like '1GB', '100MB') to bytes
size_map = {'B': 1, 'KB': 1024, 'MB': 1024**2, 'GB': 1024**3}
size_value = float(target_size[:-2])
size_unit = target_size[-2:].upper()
target_bytes = int(size_value * size_map[size_unit])
# Read input file in chunks of 1MB
chunk_size = 1024 * 1024
with open(input_file, 'rb') as source, open(output_file, 'wb') as dest:
# Read the entire source file
source_data = source.read()
source_size = len(source_data)
# Calculate how many times we need to write the source
bytes_written = 0
while bytes_written < target_bytes:
# Calculate remaining bytes
remaining = target_bytes - bytes_written
# Write either the full source or just the remaining bytes needed
write_size = min(source_size, remaining)
dest.write(source_data[:write_size])
bytes_written += write_size
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate large file by duplicating content')
parser.add_argument('input_file', help='Source file to duplicate')
parser.add_argument('output_file', help='Output file path')
parser.add_argument('size', help='Target file size (e.g., 1GB, 100MB)')
args = parser.parse_args()
try:
duplicate_file(args.input_file, args.output_file, args.size)
print(f"Successfully generated {args.output_file} with size {args.size}")
except Exception as e:
print(f"Error: {str(e)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment