[Tutor] tarfile library question
alexkleider
alexkleider at protonmail.com
Thu Dec 10 19:12:08 EST 2020
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Thursday, December 10, 2020 2:10 AM, Alan Gauld via Tutor <tutor at python.org> wrote:
> On 10/12/2020 02:30, alexkleider via Tutor wrote:
>
> > I have written the following code as an archiving utility to serve my needs:
>
> >
> > 1. is there a way of writing the code so as to eliminate the need to
> > create and populate a temporary directory and instead create the parent
> > directory and then populate it all within the "with...as tar" clause?
> >
>
> Probably. You need a loop over the target folders somewhere
> inside the open and you need to append to the tar file
> not write to it.
>
> > 2. why are <res> and <destination_directory> not considered equal?
>
> Don't know, show us some sample output.
> Also, try printing the repr() of the names as well as the str() versions.
>
> --------------------------------------------------------------------------------------------------------------------
>
> Alan G
Sorry, I didn't notice that my mail client had messed up the indentation!
Here's how it should have been:
import os
import shutil
import tarfile
def archive(destination_directory, source, targz_base_name):
"""
Create a <targz_base_name>.tar.gz archive and
file it in the <destination_directory>.
The archive is to contain all files &/or directories
listed in <source> all under a directory called <targz_base_name>.
Fails if a directory <targz_base_name> already exists.
A directory of this name is created as a temporary place to
gather what is to be archived. I'd like to avoid having to do so.
"""
os.mkdir(targz_base_name)
for folder in list_of_targets:
shutil.copytree(folder,
os.path.join(targz_base_name, folder),
)
tar_file = "{}.tar.gz".format(targz_base_name)
with tarfile.open(tar_file, "w:gz") as tar:
tar.add(targz_base_name)
shutil.rmtree(targz_base_name)
res = shutil.move(tar_file, destination_directory)
if not (repr(res) == repr(os.path.join(destination_directory,
tar_file))):
print("The following two strings:")
print("\t'{}'".format(res))
print("\t'{}'".format(os.path.join(destination_directory,
tar_file)))
print("...should be the same: something went wrong!")
The second of my questions has been resolved (silly bug of my doing!)
The first question remains:
How to avoid having to create the temporary directory targz_base_name
presumably by setting it up within the "while tarfile(... as tar:" loop.
Sorry about the confusion and thanks again in advance for any suggestions.
Alex Kleider
More information about the Tutor
mailing list