Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a zip archive of a directory?

How can I create a zip archive of a directory structure in Python?

like image 869
Martha Yi Avatar asked Dec 06 '09 11:12

Martha Yi


People also ask

How do I zip all files in a directory?

You can compress directories and the corresponding sub-directories by using the -r flag. The -r flag will tell zip to traverse the entire directory recursively.

How do I create a ZIP file with multiple folders?

Right-click on the file or folder. Select “Compressed (zipped) folder”. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.

How do I zip the current directory?

-r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

How do you zip a folder and upload it?

If you're on a PC using Windows, right-click the folder and in the menu that appears you should select Send to and then under that menu select Compressed (zipped) folder. You then should see a zip-file with the same name as the folder you created appear. You should upload this zip-file on the submission page.


1 Answers

The easiest way is to use shutil.make_archive. It supports both zip and tar formats.

import shutil shutil.make_archive(output_filename, 'zip', dir_name) 

If you need to do something more complicated than zipping the whole directory (such as skipping certain files), then you'll need to dig into the zipfile module as others have suggested.

like image 159
crdavis Avatar answered Sep 22 '22 22:09

crdavis