When I invoke add()
on a tarfile
object with a file path, the file is added to the tarball with directory hierarchy associated. In other words, if I unzip the tarfile the directories in the original directories hierarchy are reproduced.
Is there a way to simply adding a plain file without directory info that untarring the resulting tarball produce a flat list of files?
Use the tarfile module to create a zip archive of a directory. Walk the directory tree using os. walk and add all the files in it recursively.
You can use the tarfile module to read and write tar files. To extract a tar file, you need to first open the file and then use the extract method of the tarfile module.
In order to extract or un-compress “. tar. gz” files using python, we have to use the tarfile module in python. This module can read and write .
Using the arcname argument of TarFile.add() method is an alternate and convenient way to match your destination.
Example: you want to archive a dir repo/a.git/ to a tar.gz file, but you rather want the tree root in the archive begins by a.git/ but not repo/a.git/, you can do like followings:
archive = tarfile.open("a.git.tar.gz", "w|gz") archive.add("repo/a.git", arcname="a.git") archive.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With