I have tried below code to create simple tar file of all files present at given directory using python.
In directory D:/tmp/ there are some regular files only and no sub-directories inside.
import os
import tarfile
def createTar(path, tar_name):
with tarfile.open(tar_name, "w:gz") as tar_obj:
for root, dirs, files in os.walk(path):
for file in files:
tar_obj.add(os.path.join(root, file))
createTar("D:/tmp/", "D:/sample.tar.gz")
Above sample code works but it creates nested tar file like structure as D:\sample.tar.gz\sample.tar\<all files>
not D:\sample.tar.gz\<all files>
like how tar file gets created on linux using tar command.
Can someone explain why it is creating two tar files like one inside another?
You don't have a nested tar
file. You have a tar
file inside gz
file.
Your code works correctly.
gz
is its own format. It is like zip, but can hold only one single file.
tar
is an uncompressed archive format. It has no compression of its own.
So, whenever you see a file of type tar.gz
or tar.bz
or tar.xz
- you are seeing a combination of two tools - a tar
archiver and a compressor of some kind.
What ever app you are using to open your file obviously isn't designed specifically to work with compressed tar files, so it displays your file as an archive with a nested tar, which is technically true, but most modern utilities avoid showing it like this.