def make_targz(output_filename, source_dir):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
def make_targz_one_by_one(output_filename, source_dir):
tar = tarfile.open(output_filename,"w:gz")
for root,dir,files in os.walk(source_dir):
for file in files:
pathfile = os.path.join(root, file)
tar.add(pathfile)
tar.close()
make_targz("test.tar.gz","a.txt") #or make_targz_one_by_one("test.tar.gz","a.txt")