Compressing and restoring

In this section, we are going to learn about the make_archive() function of the shutil module, which will compress an entire directory. For that, we are going to write a compress_a_directory.py script and write the following content in it:

import shutil
shutil.make_archive('work', 'zip', 'work/')

Run the script as follows:

$ python3 compress_a_directory.py

In the preceding script, in shutil.make_archive() function, we passed the first argument as a name to our compressed file. zip will be our compression technique. And the, work/ will be the name of the directory that we want to compress.

Now, to restore the data from the compressed file, we are going to use the unpack_archive() function from the shutil module. Create an unzip_a_directory.py script and write the following content in it:

import shutil
shutil.unpack_archive('work1.zip')

Run the script as follows:

$ python3 unzip_a_directory.py

Now, check your directory. You will get all the contents after unzipping the directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset