Skip to content
Advertisement

how to iterate over a strict list of files with python to make a tar of them

using python3 (in a venv)

I am looking for a way to save some files, each strictly defined as its own full pathname in a variable

I would do something looking like this:

import datetime
import os
import tarfile
import time

outputfilename = "backup.configfiles"   # as far as it has datetime names can't be similar
configfilelist = [
        "/app1/DATA2/tartes/titi",
        "/app2/DATA2/tartes/toto",
        "/app3/DATA8/tartes/truc"
        ]
# and so on for all of the filenames (here is only example of list)

def definetarfilename():
        # adapt date & time to file name
        t = time.localtime()
        timestamp = str(time.strftime('%Y%m%d-%H%M',t))
        global filedatee
        filedatee=outputfilename+"."+timestamp+".tar.gz"

def writefilestotargz():
        # use compression with gz
        with tarfile.open(filedatee, "w|gz") as tar:
                for f in [configfilelist]:
                        tar.add(f)

definetarfilename()     # generate filename
writefilestotargz()     # write tar.gz file
exit()

but I get errors about :

AttributeError: "list" object has no attribute "statswith" 

I’m still new to python, so can you help define why and how I can fix that (method, syntax ?)

Advertisement

Answer

There is a minor issue in writefilestotargz function:

def writefilestotargz():
    # use compression with gz
    with tarfile.open(filedatee, "w|gz") as tar:
        for f in configfilelist:
            tar.add(f)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement