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?