Split large files using python

前端 未结 5 1119
有刺的猬
有刺的猬 2020-12-28 21:25

I have some trouble trying to split large files (say, around 10GB). The basic idea is simply read the lines, and group every, say 40000 lines into one file. But there are tw

5条回答
  •  青春惊慌失措
    2020-12-28 21:54

    chunk_size = 40000
    fout = None
    for (i, line) in enumerate(fileinput.FileInput(filename)):
        if i % chunk_size == 0:
            if fout: fout.close()
            fout = open('output%d.txt' % (i/chunk_size), 'w')
        fout.write(line)
    fout.close()
    

提交回复
热议问题