13

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.

may
  • 1,073
  • 4
  • 14
  • 31
  • 1
    Possible duplicate of [How to create a zip archive of a directory](https://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory) – Eli Korvigo Dec 21 '17 at 08:57

1 Answers1

27

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)
Neil
  • 14,063
  • 3
  • 30
  • 51