creating size-limited tar files

andrea crotti andrea.crotti.0 at gmail.com
Fri Nov 9 05:39:21 EST 2012


Anyway in the meanwhile I implemented this tar and split in this way below.
It works very well and it's probably much faster, but the downside is that
I give away control to tar and split..

def tar_and_split(inputfile, output, bytes_size=None):
    """Take the file containing all the files to compress, the bytes
    desired for the split and the base name of the output file
    """
    # cleanup first
    for fname in glob(output + "*"):
        logger.debug("Removing old file %s" % fname)
        remove(fname)

    out = '-' if bytes_size else (output + '.tar.gz')
    cmd = "tar czpf {} $(cat {})".format(out, inputfile)
    if bytes_size:
        cmd += "| split -b {} -d - {}".format(bytes_size, output)

    logger.info("Running command %s" % cmd)

    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
    out, err = proc.communicate()
    if err:
        logger.error("Got error messages %s" % err)

    logger.info("Output %s" % out)

    if proc.returncode != 0:
        logger.error("Something failed running %s, need to re-run" % cmd)
        return False



More information about the Python-list mailing list