title: Python: Assign a compression level to tarfile
body: <p>My question is a follow up to this <a href="https://stackoverflow.com/questions/5849999/how-to-make-tar-backup-using-python">one</a>.
  I would like to know how I can modify the following code so that I can assign a compression level:</p>
  
  <pre><code>import os
  import tarfile
  
  home = '//global//scratch//chamar//parsed_data//batch0'
  backup_dir = '//global//scratch//chamar//parsed_data//'
  
  home_dirs = [ name for name in os.listdir(home) if os.path.isdir(os.path.join(home, name)) ]
  
  for directory in home_dirs:
      full_dir = os.path.join(home, directory)
      tar = tarfile.open(os.path.join(backup_dir, directory+'.tar.gz'), 'w:gz')
      tar.add(full_dir, arcname=directory)
      tar.close()
  </code></pre>
  
  <p>Basically, what the code does is that I loop through each directory in <code>batch0</code> and compress each directory (where in each directory there are 6000+ files) and create a <code>tar.gz</code> compressed file for each directory in <code>//global//scratch//chamar//parsed_data//</code>. 
  I think by default the compression level is = 9 but it takes a lot of time to compressed. I don't need a lot of compression. A level 5 would be enough. How can I modify the above code to include a compression level?</p>