Example-1: Compressing a single file
Create a new file named ‘zipcode1.py’ and add the following code. zipfile module is imported to compress the file. temp.zip is assigned as zip file name with write mode and next, the original filename, temp.txt and compress type are given as parameters in the write method.
zip_file = zipfile.ZipFile('temp.zip', 'w')
zip_file.write('temp.txt', compress_type=zipfile.ZIP_DEFLATED)
zip_file.close()
Run the script
The size of temp.txt is 27 bytes and after compression, the size of temp.zip is 2 bytes.
Example-2: Compressing a particular directory
Create a new file named ‘zipcode2.py’ and add the following code. A directory may contains many files, folders and subfolders. To read the content of the directory, os module of python is imported with zipfile module to compress the directory. In this script, mydir directory is used for compression.
import os
import zipfile
# Declare the function to return all file paths of the particular directory
def retrieve_file_paths(dirName):
# setup file paths variable
filePaths = []
# Read all directory, subdirectories and file lists
for root, directories, files in os.walk(dirName):
for filename in files:
# Create the full filepath by using os module.
filePath = os.path.join(root, filename)
filePaths.append(filePath)
# return all paths
return filePaths
# Declare the main function
def main():
# Assign the name of the directory to zip
dir_name = 'mydir'
# Call the function to retrieve all files and folders of the assigned directory
filePaths = retrieve_file_paths(dir_name)
# printing the list of all files to be zipped
print('The following list of files will be zipped:')
for fileName in filePaths:
print(fileName)
# writing files to a zipfile
zip_file = zipfile.ZipFile(dir_name+'.zip', 'w')
with zip_file:
# writing each file one by one
for file in filePaths:
zip_file.write(file)
print(dir_name+'.zip file is created successfully!')
# Call the main function
if __name__ == "__main__":
main()
Run the script
The size of mydir is 21 bytes and after compression, the size of mydir.zip is 2 bytes.
Example-3: Compressing a directory given by command line argument
Create a new file named ‘zipcode3.py’ and add the following code. To read the command line value, another python module sys is imported with os and zipfile modules.
import os
import sys
import zipfile
# Declare the function to return all file paths of a particular directory
def retrieve_file_paths(dirName):
# setup file paths variable
filePaths = []
# Read all directory, subdirectories and file lists
for root, directories, files in os.walk(dirName):
for filename in files:
# Create the full filepath by using os module.
filePath = os.path.join(root, filename)
filePaths.append(filePath)
# return all paths
return filePaths
# Declare the main function
def main():
# Check two arguments are given at the time of running the script
if len (sys.argv) != 2 :
print ("You have enter the name of the directory to zip")
sys.exit (1)
# Set the directory name from command argument
dir_name = sys.argv[1]
# Set the zip file name
zipFileName = dir_name + ".zip"
# Call the function to retrieve all files and folders of the assigned directory
filePaths = retrieve_file_paths(dir_name)
# print the list of files to be zipped
print('The following list of files will be zipped:')
for fileName in filePaths:
print(fileName)
# write files and folders to a zipfile
zip_file = zipfile.ZipFile(zipFileName, 'w')
with zip_file:
# write each file seperately
for file in filePaths:
zip_file.write(file)
print(zipFileName+' file is created successfully!')
# Call the main function
if __name__ == "__main__":
main()
Run the script
test is given as directory name in the command line argument. The size of test is 21 bytes and after compression, the size of test.zip is 2 bytes.
I hope, this tutorial will help you to use python for compressing any file or directory.