Skip to content

Instantly share code, notes, and snippets.

@daryltucker
Created August 24, 2014 18:54
Show Gist options
  • Save daryltucker/a06d41e93d3cc7cae54e to your computer and use it in GitHub Desktop.
Save daryltucker/a06d41e93d3cc7cae54e to your computer and use it in GitHub Desktop.
python - tarfile - Relative Paths
import tarfile
import glob
import tempfile
FILENAME = 'Title_for_your_file'
t_dir = t_dir = tempfile.mkdtemp(prefix='app-')
# Compress t_dir
tarname = '/tmp/%s.tar.bz2' % (FILENAME)
fileobj = open(tarname, 'wb')
tar = tarfile.open(tarname, mode='w:bz2', fileobj=fileobj)
for filex in glob.glob(t_dir):
if filex:
arcname = re.sub(t_dir, '%s/' % (FILENAME), filex)
tar.add(filex, arcname=arcname)
tar.close()
fileobj.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment