How to create a tar file using Python?

ProgrammingPythonServer Side Programming

Tape Archive Files is what the letter TAR in tar files stands for. Tar files are the archive files which allows you to store numerous files in a single file. Open-source software is distributed using tar files.

Tar files typically end in.tar, but after they've been compressed using tools like gzip, they have the ending tar.gz.

Various file modes to open Python tar file

  • r − reads a TAR file by opening it.
  • r − Reads an uncompressed TAR file when it is opened.
  • w or w − Opens a TAR file for uncompressed writing
  • a or a − Opens a TAR file for appending without compression.
  • r:gz − opens a TAR file that has been compressed with gzip for reading.
  • w:gz − opens a TAR file that has been compressed with gzip for writing.
  • r:bz2 − opens a TAR file with bzip2 compression for reading.
  • w:bz2 − opens a TAR file with bzip2 compression for writing.

Creating a tar file using Python

Using the tarfile module in Python, tar files can be produced. Add more files to the tar file after opening a file in write mode.

Using open() method

An example of Python code that creates a tar file is shown below. Here, we create a tar file using the open() method .The open() method here accepts "w" to open the file in write mode along with the filename of the tar file that will be produced as its first parameter.

Example

Following is an example to create a tar file using open() method −

#importing the module import tarfile #declaring the filename name_of_file= "TutorialsPoint.tar" #opening the file in write mode file= tarfile.open(name_of_file,"w") #closing the file file.close()

Output

As an output we can see a tar file created with the name “TutorialsPoint”.

Example

Note − We can add the files in the created tar file using add() method. The example is as shown below −

#importing the module import tarfile #declaring the filename name_of_file= "TutorialsPoint.tar" #opening the file in write mode file= tarfile.open(name_of_file,"w") #Adding other files to the tar file file.add("sql python create table.docx") file.add("trial.py") file.add("Programs.txt") #closing the file file.close()

Output

As an output we can see a tar file created with the name “TutorialsPoint”.The filename of the file to be added is passed as an input to the add() method. 

Creating and listing the files using os.listdir() method

A list of each file and folder in a directory is returned by the listdir() method.

Example

Following is an example to create a tar file using os.listdir() method −

import os import tarfile #Creating the tar file File = tarfile.open("TutorialsPoint.tar", 'w') files = os.listdir(".") for x in files: File.add(x) #Listing the files in tar for x in File.getnames(): print ("added the files %s" % x) File.close()

Output

We get the following output along with the creation of tar file −

added the files desktop.ini
added the files How to create a tar file using Python.docx
added the files Microsoft Edge.lnk
added the files prateek
added the files prateek/Prateek_Sarika.docx
added the files prateek/sample (no so good just follow the template).docx
added the files untitled.py
added the files ~WRL0811.tmp

Creating tar archives using tarfile and os.walk() method in Python

To construct a zip archive out of a directory, use the tarfile module. Use the os.walk command to iteratively add each file in the directory tree.

Example

Following is an example to create tar archives −

import os import tarfile def tardirectory(path,name): with tarfile.open(name, "w:gz") as tarhandle: for root, dirs, files in os.walk(path): for f in files: tarhandle.add(os.path.join(root, f)) tardirectory('C:\Users\Lenovo\Downloads\Work TP','TutorialsPoint.tar.gz') tarfile.close()

Output

As an output we can see a tar file created with the name “TutorialsPoint”.

raja
Updated on 18-Aug-2022 12:09:00

Advertisements