Skip to content

Instantly share code, notes, and snippets.

@ZavierChambers
Created November 23, 2025 23:49
Show Gist options
  • Select an option

  • Save ZavierChambers/09b4c14119215c57b008aaa75407ad3a to your computer and use it in GitHub Desktop.

Select an option

Save ZavierChambers/09b4c14119215c57b008aaa75407ad3a to your computer and use it in GitHub Desktop.
Generates a file of precise size using random bytes.
import os
def make_random_file(path, target_size_bytes, chunk_size=1024 * 1024):
"""
Create a file filled with random bytes until it reaches target_size_bytes.
chunk_size just controls memory use while streaming!!!!
1 = Byte 1 KB = 1024**1, 1 MB = 1024**2, 1 GB = 1024**3
"""
written = 0
with open(path, "wb") as f:
while written < target_size_bytes:
remaining = target_size_bytes - written
n = min(chunk_size, remaining)
f.write(os.urandom(n))
written += n
print(f"Created {path}, size = {written} bytes")
# Example usage (UNCOMMENT BELOW)!!!:
#make_random_file("random.bin", ((1024 ** 1) * 1 )) # REMEMBER TO PASS IN BYTES!!! (1024 ** x) * y where x is the powers from kb, mb, gb, etc & y is the amount of that unit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment