Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
Created April 17, 2014 10:25
Show Gist options
  • Save stevedoyle/10971811 to your computer and use it in GitHub Desktop.
Save stevedoyle/10971811 to your computer and use it in GitHub Desktop.
Compress individual files with gzip
#!/usr/bin/env python
import argparse
import subprocess
import shlex
import sys
def compress_file(filename, level):
try:
cmd = shlex.split("gzip %s -c %s" % (level, filename))
subprocess.call(cmd, stdout=file(filename+'.gz', 'wb'))
except:
print "Error:", sys.exc_info()[0]
def main():
options = { 'level' : '-1' }
parser = argparse.ArgumentParser(description='Compress some files individually')
parser.add_argument('files', metavar='FILE', nargs='+',
help='A file to compress')
parser.add_argument('--level', nargs='?', default=-1, choices=['-1','-9'],
help='Compression level: -1 fastest, -9 smallest')
args = parser.parse_args()
for f in args.files:
compress_file(f, args.level)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment