Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?)

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading