import os lines_per_file = 300 smallfile = None with open('/home/marco/Py/really_big_file.txt') as bigfile: for lineno, line in enumerate(bigfile): if lineno % lines_per_file == 0: if smallfile: smallfile.close() small_filename = 'small_file_{}.txt'.format(lineno + lines_per_file) smallfile = open(small_filename, "w") smallfile.write(line) if smallfile: smallfile.close()