splittxt.py 459 B

1234567891011121314
  1. import os
  2. lines_per_file = 300
  3. smallfile = None
  4. with open('/home/marco/Py/really_big_file.txt') as bigfile:
  5. for lineno, line in enumerate(bigfile):
  6. if lineno % lines_per_file == 0:
  7. if smallfile:
  8. smallfile.close()
  9. small_filename = 'small_file_{}.txt'.format(lineno + lines_per_file)
  10. smallfile = open(small_filename, "w")
  11. smallfile.write(line)
  12. if smallfile:
  13. smallfile.close()