"""
From: Python Cookbook ZhCN.pdf
Thanks for Ed Gordon, Ravi Teja Bhupatiraju
"""

import tarfile, os

def make_tar(folder_to_backup, dest_folder, compression='bz2'):
	if compression:
		dest_ext = '.' + compression
	else:
		dest_ext = ''

	arcname = os.path.basename(folder_to_backup)
	dest_name = '%s.tar%s' % (arcname, dest_ext)
	dest_path = os.path.join(dest_folder, dest_name)
	if compression:
		dest_cmp = ':' + compression
	else:
		dest_cmp = ''
	out = tarfile.TarFile.open(dest_path, 'w'+dest_cmp)
	out.add(folder_to_backup, arcname)
	out.close()
	return dest_path

if __name__ == '__main__':
	make_tar('./direc', '.')
