pythoncsvzip

Zip list of files python


I have created some csv files in my code and I would like to zip them as one folder to be sent by e-mail. I already have the e-mail function but the problem is to zip. I tried to use this: here I am not extracting or find the files in a directory. I am creating the program the csv files and making a list of it. My list of files is like this:

lista_files = [12.csv,13.csv,14.csv]

It seems to be easy for developers but as a beginning it is hard. I would really appreciate if someone can help me.


Solution

  • I believe you're looking for the zipfile library. And given that you're looking at a list of filenames, I'd just iterate using a for loop. If you have directories listed as well, you could use os.walk.

    import zipfile
    
    lista_files = ["12.csv","13.csv","14.csv"]
    with zipfile.ZipFile('out.zip', 'w') as zipMe:
        for file in lista_files:
            zipMe.write(file, compress_type=zipfile.ZIP_DEFLATED)