How to zip all outputs (*.csv and *.tiff) in jupyter notebook

Question:

I use this command

! zip -r results.zip . -i *.csv *.pdf

in Jupyter Notebook(Python 3.7.10) in order to zip all the output files. But, it shows

zip’ is not recognized as an internal or external command, operable program or batch file.

Can anyone suggest what I miss?

Asked By: tassaneel

||

Answers:

I think you are trying to use,os.system():
If you are using linux

import os 
os.system("zip -r results.zip . -i *.csv *.pdf")
#OR
import subprocess
subprocess.Popen("zip -r results.zip . -i *.csv *.pdf")

If you aren’t using linux, in windows. There is library called zipfile, you can use it:

from zipfile import ZipFile
import os

filesname=os.listdir("<path or empty") # Or you can alternately use glob inside `with` ZipFile
# Empty means working folder 

with ZipFile('output.zip', 'w') as myzip:
    for file in files:
        if file.endswith(".csv") and file.endswith(".pdf"):
            myzip.write(file)
Answered By: Xitiz

On modern Windows the Zip tool is Tar

It is not well documented nor as easy to use as most 3rd Party zippers thus you would need to custom wrap your OS call.

generally the following should be good enough

Tar -a -cf results.zip *.csv *.pdf

However if there are not one or other type the response will complete for the valid group, but with a very cryptic response:-

Tar: : Couldn't visit directory: No such file or directory
Tar: Error exit delayed from previous errors.
Answered By: K J
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.