CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2022-07-07
by Pikkostack

Original Post

Original - Posted on 2009-12-06
by Mark Byers



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Slightly modified answer from here: https://stackoverflow.com/questions/46229764/python-zip-multiple-directories-into-one-zip-file ```python import os import zipfile

def zipdir(path, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(path): for file in files: ziph.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(path, '..')))

def zipit(zip_dir, sub_dir_list): zipf = zipfile.ZipFile(zip_dir + '.zip', 'w', zipfile.ZIP_DEFLATED) for sub_dir in sub_dir_list: zipdir(sub_dir, zipf) zipf.close()
zip_dir = '/folderA' sub_dir_list = ['a1', 'a2'] zipit(zip_dir, sub_dir_list) ``` This creates a `/folderA.zip` including the specified sub directories.
As others have pointed out, you should use [zipfile](http://docs.python.org/library/zipfile.html). The documentation tells you what functions are available, but doesn't really explain how you can use them to zip an entire directory. I think it's easiest to explain with some example code:
```python import os import zipfile def zipdir(path, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(path): for file in files: ziph.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(path, '..')))
with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf: zipdir('tmp/', zipf) ```

        
Present in both answers; Present only in the new answer; Present only in the old answer;