Python create tar file

How to create a tar file using Python?

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.
Читайте также:  Css несколько картинок выровнять

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”.

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()

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”.

Источник

Python Tarfile

The TAR file format is used to store or archive several files into one file. This is very useful when sending or transmitting a large quantity/bundle of files to the network or when we need to archive the older files. For the UNIX operating system, the “tar” utility is first introduced. Its goal is to compile numerous files into one archive file, frequently referred to as a tarball, making it simple to distribute the data. The tarfile module of Python’s standard library contains functions that assist in creating tar files or archives and extracting data as needed from the tarball. The archives can either be created with no compression at all or using gzip, bz2, and lzma.

What Is the Tarfile Module in Python?

We don’t need to install the tarfile module explicitly because it is part of the Python standard library; all we need to do is “import” it. Reading and writing tar archives can be made much simpler than it should be by the methods provided by the “tarfile” Python module. The built-in “tarfile” module in Python provides a powerful interface for writing and reading tar files.

File Modes to Open the Python TAR Files

The following are some file modes that can be used in Python to open a tarfile:

r: Opens a TAR file to read.

r: Opens a TAR file and reads an uncompressed version of it.

w or w: Enables uncompressed writing by opening a TAR file.

a or a: Opens a TAR file so the data can be appended to it without compression.

r:gz: Opens a gzip-compressed TAR file for reading.

w:gz: Opens a gzip-compressed TAR file for writing.

r:bz2: Opens a bzip2 compressed TAR file for reading.

w:bz2: Opens a bzip2 compressed TAR file for writing.

Now, let’s use the tarfile module to understand how it works in Python.

Creating a Tar File

The tarfile module in Python enables us to create tar files. First, open a tar file in write mode. Then, add a bundle of files or a single file to the tar file.

Example 1: Creating a Tar File Using the Open() Function

Here, we use the open() function to create a tar file and the add() method to add other files to the tar file.

Here, the first input to the open() method is the name of our tar file that has to be created, followed by the “w” argument to open the file (tarfile) in write mode. The file’s name that has to be added or archived to the “.tar” file is passed inside the add() method as input. We archive three files into our tar file using the add() function on a file object

Example 2: The Os.Listdir() Function to Create and List Files

The listdir() method returns a list of files and directories that reside in a specified directory. Using the tarfile module, we first create our tarfile. Then, we add the files into our tar file using the add() function. After adding the files to the tar file, we obtain a list of files that have been archived in the “.tar” file using the os.listdir() function. To use this function, we have to import the OS module.

The tar file “folder” is created by archiving all the files stored in the “data” folder. The getnames() function returns the file names that are stored in the tar file “folder”.

How to Verify Whether a File Is Tar or Not

Using the is_tarfile() method in the tarfile module, we can quickly determine whether a file has the “.tar” extension.

We pass the name of the “My_file.tar” file in the is_tarfile() module to check whether it is a tar file or not. The function returns True, which means that the specified file is a tar file. Let’s check for another file.

Since “note.txt” is not a tar file, the function returns False as output.

Reading a Tar File

The tarfile library can help you in reading tar files without extracting them first. The tarfile.open(filepath, mode) can be used to open a tar file in Python. The file/path is the absolute or relative location of the file that we want to read. Different kinds of parameters may be used in place of the mode.

We can also read the open file in compress mode. A compression method may optionally be combined with the operation mode. Thus, mode[:compression] becomes the new syntax. The abbreviations that support the compression methods are as follows:

In the previous code, we first open a bz2 compressed TAR file for writing. Then, we open the bz2 compressed TAR file for reading.

Checking the Content of the Tar File

Using the getnames() function of the tarfile module, we can view a tar file’s contents without extracting them. The list of file names is returned by the getnames() function of the tarfile module. Here, we open the file in “read” mode so “r” is specified as the second argument in the open() function.

We oepn a “project.tar” file in reading mode. The getnames() function returns the names of files that are archived in the “project.tar” file. To print the content/file names, the for-loop is used to iterate over the files in the tar file.

Appending Files to Tar File

Using the add() function from the tarfile module, we can directly add files to a tar file, just like we did when we created the tar file. Unlike prior examples, “a” is passed to the open() method as the second argument because the file must be opened in append mode.

We use the append mode to open the “project.tar” file. Five files are already present in the “paoject.tar” file. We append three more files using the add() function on the file object.

Extracting a Specific File from a Tar Archive File

The extractfile() function of the tarfile library can be used to extract one file from an archived folder. This method accepts the name of the file as input and extracts the specified file into our working directory. If you want to extract more than one file, you have to call the extractfile() multiple times. While extracting a member as a file object from the archive, a member could be a TarInfo object or a filename. An “io.BufferedReader” object returns if a member is a normal file or a link. None is presented for all other existing/current members. The KeyError is raised if a member is absent in the archive file.

The “books.xml” file is extracted from the “project.tar” file.

Extracting All Files from the Tar File

The extractall() function of the tarfile library can be used to extract the whole zipped file rather than just one particular file. The image that follows shows the directory before the files of the archived file are extracted.

The whole contents of the tar file is extracted into the folder in our current directory, the “data”, by the extractall() method. Now, let’s extract the files of the archived file to this folder.

The function successfully extracted all the files to the specified folder.

Conclusion

In this article, we learned what tar files or tar archives are and how to use Python’s tarfile module to create, access, and manage the tar files. We learned the file modes which we can use to open the tar files in reading, writing, and compression modes. We explained the methods to list the files after archiving them in tar. We discussed how to append the files in the tar file. Also, we learned how a specific file or all files can be extracted into the current directory.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Источник

Оцените статью