Search code examples
pythonpython-3.xsplitsizelarge-files

Python: how to get part of file by size


I would like to read just one part (not chunks) from a txt-file (10GB) with lines and write them into another file. The size of the part should be exactly 25MB.

I have tried with linecache.getlines, but it was not very exactly. Thanks.


Solution

  • A simple way to perform the split is to use read(), assuming each character is a byte.

    for nameadd in range(10*1024/25):
        f = open('fname.txt')
        saveTxt = f.read(25*(1024**2))
        fSave = open(str(nameadd)+'fname.txt','w')
        fSave.write('%s',saveTxt)