Created
August 24, 2014 18:54
-
-
Save daryltucker/a06d41e93d3cc7cae54e to your computer and use it in GitHub Desktop.
python - tarfile - Relative Paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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