Using the tarfile module to create TAR archives

This section will help you to learn about how we can create tar archives using Python's tarfile module.

 The tarfile module is used to read and write tar archives using gzip, bz2 compression techniques. Make sure the necessary files and directories are present. Now, create a tarfile_example.py script and write the following content in it:

import tarfile
tar_file = tarfile.open("work.tar.gz", "w:gz")
for name in ["welcome.py", "hello.py", "hello.txt", "sample.txt", "sample1.txt"]:
tar_file.add(name)
tar_file.close()

Run the script as follows:

$ python3 tarfile_example.py

Now, check your present working directory; you will see work.tar.gz has been created.

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

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