Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
tarfile is a python library to read and write gzip`/`bz2 compressed files.

How to read a gzip compressed tar archive and display some member information:

import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
if tarinfo.isreg():
print "a regular file."
elif tarinfo.isdir():
print "a directory."
else:
print "something else."
tar.close()


Create a compressed file:

with tarfile.open(dst, "w:gz") as tar:
print("Archiving " + src + " into " + dst)
tar.add(src, arcname = os.path.basename(src))

NOTE: the flag of w:gz opens the destination in write mode. Used to create a new tar file.

#python #tarfile #tar #bz2 #gzip