Break large file down into multiple files

redbaron ivanov.maxim at gmail.com
Fri Feb 13 03:02:56 EST 2009


> New to python.... I have a large file that I need to break up into
> multiple smaller files. I need to break the large file into sections
> where there are 65535 lines and then write those sections to seperate
> files.

If your lines are variable-length, then look at itertools recipes.

from itertools import izip_longest

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

with open("/file","r") as f:
    for lines in grouper(65535,f,""):
        data_to_write = '\n'.join(lines).rstrip("\n")
        ...
        <write data where you need it here>
        ...




More information about the Python-list mailing list