How can I create a zip archive of a directory structure in Python?
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.
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”.
-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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With