1. import os
  2. import random
  3. import string
  4. from concurrent.futures import ThreadPoolExecutor
  5. def generate_random_filename(length=10):
  6. letters = string.ascii_lowercase
  7. return ''.join(random.choice(letters) for i in range(length))
  8. def write_random_to_file(file_path, size):
  9. with open(file_path, 'wb') as file:
  10. file.write(os.urandom(size))
  11. def create_files(file_count, file_size):
  12. os.makedirs('test', exist_ok=True)
  13. params = [
  14. ('test/' + generate_random_filename(20) + '.txt', file_size)
  15. for _ in range(file_count)
  16. ]
  17. with ThreadPoolExecutor(max_workers=None) as executor:
  18. list(executor.map(lambda param: write_random_to_file(*param), params))
  19. if __name__ == "__main__":
  20. # 生成文件数量
  21. file_count = 2000
  22. # 单个文件大小,单位字节 1024=1kb
  23. file_size = 1024 * 1024
  24. create_files(file_count, file_size)