How to compress files in a directory using Python
System Admins are always faced with disk space issues.In most cases compressing a directory/directories will help conserve some disk space.In this tutorial i will demonstrate how to use python tarfile module to compress a directory. I assume the admin already has some python programming knowledge.
We start by importing the tarfile module which is included by default in most python versions.We use python 2.7 in the script below.The script below will compress all files in the /home directory and save it at /root/backup directory.
#!/usr/bin/env python import tarfile import os file_name = "/root/backup/home.tar.gz" tar = tarfile.open(file_name, "w:gz") os.chdir("/home") for name in os.listdir("."): tar.add(name) tar.close()
Lets break the script down.
-
- we first defined out shebang which point to the python environment.
- we then imported the tarfile and os mdoules
- we then defined a variable which will hold the filename of the compressed directory and that is home.tar.gz which will be located in the /root/backup folder.
- The next code which is tar = tarfile.open(file_name, “w:gz”) will create the file_name already defined and write to it using the gz compression method.
- We then change directory to the directory where we will be compressing which is /home
- Next we use a for loop to list the files and folders in the /home directory and then add those files and folders using the tar.add(name).
- Finally we close our script with the tar.close() code
Sometimes also its also important to add date to filenames to differentiate itself from others.
In that case we need to import the datetime module and add date to our file name.The script will now look this this:
#!/usr/bin/env python import tarfile import os import datetime now = datetime.date.today() file_name = "/root/backup/home-{}.tar.gz".format(now) tar = tarfile.open(file_name, "w:gz") os.chdir("/home") for name in os.listdir("."): tar.add(name) tar.close()
The new code introduced is now = datetime.date.today() – This declares now as variable and holds the date for that day.
We then format our file name to include the date for the day – file_name = “/root/backup/home-{}.tar.gz”.format(now)
All compressed files will now include a date to make it easier to read.This is very important when a cron job is set to compress this files.
You can save file script now, for example backup.py and run it.You can also set cron job to be doing this on regular basis.