split.py 713 B

12345678910111213141516171819202122
  1. import json
  2. def split_file(input_filename):
  3. chunk_size = 102400 # 100kB
  4. part_number = 1
  5. parts = []
  6. with open(input_filename, 'rb') as input_file:
  7. while True:
  8. data = input_file.read(chunk_size)
  9. if not data:
  10. break
  11. output_filename = f'card_db_parts/part{part_number}'
  12. with open(output_filename, 'wb') as output_file:
  13. output_file.write(data)
  14. parts.append(f'part{part_number}')
  15. part_number += 1
  16. with open("card_db_parts/index.json", 'w') as fp:
  17. fp.write(json.dumps(parts))
  18. print(f"file splited to {part_number-1} parts")
  19. # 示例用法
  20. split_file('card_db.json')