py-test/testGeneratefile.py

33 lines
867 B
Python

import os
import random
# 输出目录
output_dir = "./output_files"
os.makedirs(output_dir, exist_ok=True)
# 配置
file_count = 40
min_size_mb = 70
max_size_mb = 80
chunk_size = 1024 * 1024 # 每次写入 1MB
def generate_random_file(file_path, size_bytes):
with open(file_path, "wb") as f:
written = 0
while written < size_bytes:
chunk = os.urandom(min(chunk_size, size_bytes - written))
f.write(chunk)
written += len(chunk)
for i in range(1, file_count + 1):
file_size_mb = random.randint(min_size_mb, max_size_mb)
file_size_bytes = file_size_mb * 1024 * 1024
filename = f"file_{i}.bin"
filepath = os.path.join(output_dir, filename)
print(f"Creating {filename} ({file_size_mb} MB)...")
generate_random_file(filepath, file_size_bytes)
print("✅ 所有文件生成完毕。")