MoonPoint Support Logo


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals

Advanced Search
Sun Mon Tue Wed Thu Fri Sat

Sun, Nov 13, 2016 10:25 pm

Python script to create a zip file containing all files in a directory

The Python code below will create a zip file from the files and any subdirectories within a specified directory. I.e., it will recursively compress the files within a directory.


# Name:
# Version: 1.0
# Created: 2016-11-13
# Last modified: 2016-11-13
# Purpose: Creates a zip file given a directory where the files to be zipped
# are stored and the name of the output file. Don't include the .zip extension
# when specifying the zip file name.
# Usage: output_filename dir_name
# Note: if the output file name and directory are not specified on the 
# command line, the script will prompt for them.

import sys, shutil

if len(sys.argv) == 1:
   dir_name = raw_input("Directory name: ")
   output_filename = raw_input("Zip file name: ")
elif len(sys.argv) == 3:
   output_filename = sys.argv[1]
   dir_name = sys.argv[2]
   print "Incorrect number of arguments! Usage: output_filename dir_name"

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

The script takes two arguments: the output file name and the directory to be compressed. The .zip extension should not be included with the output file name; it will automatically be appended to the output file name. If no arguments are specified on the command line, the script will prompt for them. E.g., if I wanted to create a zip file named from the contents of the folder Example, I could use the command python test Example or, if the script is made executable on a Linux or Apple OS X system with chmod u+x, then ./ test Example can be used.

[/languages/python] permanent link

Once You Know, You Newegg AliExpress by

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo