Automate Bulk Folder Compression and Cleanup with Python in Minutes
This tutorial shows how to use Python's os, shutil, and zipfile modules to batch‑compress thousands of folders, delete the original files, and produce zip archives automatically, turning a labor‑intensive task into a one‑minute automated process.
Introduction
Hello, I am Cui Yanfei. A client needed to compress files in thousands of directories, delete the original files, and keep only the compressed archives. Doing this manually would be time‑consuming, so Python is the perfect tool.
Project Goal
Batch compress the contents of folders to meet the client’s requirements.
Project Preparation
Software: PyCharm
Required libraries: os, shutil, zipfile
Project Analysis
1) How to read source files?
Use the os library to get a list of folder names and loop through them to obtain the files to be compressed.
2) How to perform compression?
Use zipfile.ZipFile() from the zipfile library to compress the obtained files.
3) How to delete source files?
First use os.remove() to delete files, then use shutil.rmtree() to delete empty directories.
Implementation
Step 1: Import required libraries
import os as os
import shutil
import zipfileStep 2: Define functions for deleting files and compressing directories
def del_(rootdir):
filelist = []
filelist = os.listdir(rootdir) # list all file names in the directory
for f in filelist:
filepath = os.path.join(rootdir, f) # map file name to absolute path
if os.path.isfile(filepath): # if it is a file
os.remove(filepath) # delete the file
elif os.path.isdir(filepath):
shutil.rmtree(filepath, True) # delete the folder and its contents
shutil.rmtree(rootdir, True)
def zipDir(dirpath, outFullName):
zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
for path, dirnames, filenames in os.walk(dirpath):
# remove the base path, compress only files/folders under the target directory
fpath = path.replace(dirpath, '')
for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()Step 3: Create the main function
def main():
path_end = 'D:/a/h/'
date = os.listdir(path_end) # get list of all sub‑folders
for f in date:
ljbc = 'D:/a/h/' + f + '/' + '查询信息.zip' # compressed file name 1
ljbc2 = 'D:/a/h/' + f + '/' + '下发修改.zip' # compressed file name 2
ljcx = 'D:/a/h/' + f + '/' + '查询信息' # source folder 1
ljxf = 'D:/a/h/' + f + '/' + '下发修改' # source folder 2
zipDir(ljcx, ljbc)
zipDir(ljxf, ljbc2)
del_(ljcx)
del_(ljxf)
if __name__ == '__main__':
main()Result Showcase
Processed folders:
Compressed files inside the processed folders:
Conclusion
This article demonstrated how to use Python to batch compress a large number of files with just a few lines of code; once the script runs, the task that would take a person a whole day is completed in under a minute. Life is short—let Python do the heavy lifting!
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
