Split equivalent of gzip files in python

I am trying to replicate this bash command to bash, which returns every file of 180 MB each.

split -b 50m "file.dat.gz" "file.dat.gz.part-"

My python equivalent attempt

import gzip
infile = "file.dat.gz"
slice = 50*1024*1024 # 50MB
with gzip.open(infile, 'rb') as inf:
  for i, ch in enumerate(iter(lambda: inf.read(slice), "")):
    print(i, slice)
    with gzip.open('{}.part-{}'.format(infile[:-3], i), 'wb') as outp:
      outp.write(ch)

This returns 15 MB each gzipped. When I delete files, then they are 50 MB each.

How to split a gzipped file in python to split 50 MB files each before launch?

+4
source share
1 answer

, split , . gzip gzip. gunzip , . , , . , Python, - :

infile_name = "file.dat.gz"

chunk = 50*1024*1024 # 50MB

with open(infile_name, 'rb') as infile:
    for n, raw_bytes in enumerate(iter(lambda: infile.read(chunk), b'')):
        print(n, chunk)
        with open('{}.part-{}'.format(infile_name[:-3], n), 'wb') as outfile:
            outfile.write(raw_bytes)

, .

, , . - bytesIO, gunzip gzip , , bytesIO.

, , , .

+1

Source: https://habr.com/ru/post/1682008/


All Articles