Q:

python splitting text file by line reminders

I have created a python program which count the lines in a text file then split it into multi files depending on how much file numbers the user asks for but i have a problem with the remainder of the division process like when the user asks to split the file into 3 files the script creates 4 files and write the remaining lines in the fourth file
My Code :
num_lines = sum(1 for line in open("Perfix/{0}.txt".format(name)))
size2 = num_lines / progs
with open('Perfix/{0}.txt'.format(name)) as f:
    for i, g in enumerate(grouper(size2, f, fillvalue=''), 0):
        with open('ip{0}_{1}'.format(name,(i+1)), 'w') as fout:
           fout.writelines(g)

any way how i can solve that ?
Thanks

A:

If you do want the extra lines to be written to the last file, your size should be different for the last one. One way is you can build an array with all file sizes and if there is a remainder, append that part to the size of last file. Seems your grouper is a custom function extending itertools. I don't know what it does, but here is some code with out that. I just read as many lines as I need from the initial file:
def fileSplit(name,progs):
    num_lines = sum(1 for line in open("{0}.txt".format(name)))
    extra=num_lines%progs #save the remainder length
    sizes=[num_lines / progs ] * progs
    sizes[-1]=sizes[-1]+extra #add remainder lines to the length for last file
    with open('{0}.txt'.format(name)) as f:
        for i,g in enumerate(sizes):
            with open('ip{0}_{1}'.format(name,(i+1)), 'w') as fout:
               #read g lines from f at a time and add them to your new fout
               fout.writelines([next(f) for x in xrange(g)])

