We’ve all been there: you’re knee-deep in a project, and see a tar file that needs to be opened. Without the key, it’s like discovering a treasure trove! Don’t worry; Python is a reliable companion prepared to hold the key to your riches! The helper will assist you in opening a tar file and exposing its hidden treasures. So, grab your coding gear, and let’s dive into the guide on how to work with tar files in Python.
Table of Contents
What is TAR Files?
TAR stands for Tape Archive Files and this tar file is used to bundle a set of files into a single file, this is helpful when archiving older files or sending bulk files over the bank.
Python has a standard tarfile module that can be used to work with tar files. This module supports gzip, lzma, and bz2 compressions.
How to Read a TAR File in Python
For reading tar files in Python we have to use tarfile.open
that will return an tarfile.TarFile
object. This function’s arguments are the filename and operation modes like write and read modes.
Example:
import tarfile
with tarfile.open("firstSample.tar", "r") as tf:
print("Opened Tar File")
PythonHow to Extract TAR File Content in Python
Above we see, how we can open a tar file using Python, now we will go for how we can extract tar file content. Extraction of tar file done using tarfile.Tarfile.extractall
the method. Path and members are arguments that are accepted by this tarfile.Tarfile.extracttall
method.
Path: Path to a directory to which a tar file should be extracted.
Members: specify the files to be extracted, it should be a subset of tarfile.TarFile.getmembers()
output,
Example:
import tarfile
with tarfile.open("firstSample.tar", "r") as tf:
print("Opened tarfile")
tf.extractall(path="./extraction_directory")
print("All files extracted")
PythonHow to Extract Single TAR File in Python
To extract specific files from a tar file, simply pass a reference to the file object or the file path as a string to the tarfile.TarFile.extract
method.
If you want to see a list of all files inside a tar file, use the tarfile.TarFile.getmembers
method. This will return a list of tarfile.TarInfo
instances, each representing a file in the archive.
import tarfile
with tarfile.open("./firstSample.tar", "r") as tf:
print("Opened tarfile")
print(tf.getmembers())
print("Members listed")
PythonOutput:
Opened tarfile
[<TarInfo 'sample' at 0x7fe14b53a048>, <TarInfo 'sample/sample_txt1.txt' at 0x7fe14b53a110>, <TarInfo 'sample/sample_txt2.txt' at 0x7fe14b53a1d8>, <TarInfo 'sample/sample_txt3.txt' at 0x7fe14b53a2a0>, <TarInfo 'sample/sample_txt4.txt' at 0x7fe14b53a368>]
Single File Extraction
import tarfile
file_name = "firstSample/firstSample1.txt"
with tarfile.open("firstSample.tar", "r") as tf:
print("Opened tarfile")
tf.extract(member=file_name, path="./extraction_directory")
print(f"{file_name} extracted")
PythonHow to Write a TAR File in Python
If you want to merge a file to a tar file, then we will open the file in append mode and use tarfile.TarFile.add
method takes the file path to be added as a parameter.
import tarfile
file_name = "firstSample1.txt"
with tarfile.open(f"./firstSample.tar", "a") as tf:
print("Opened tarfile")
print(f"Members before addition of {file_name}")
print(tf.getmembers())
tf.add(f"{file_name}", arcname="sample")
print(f"Members after addition of {file_name}")
print(tf.getmembers())
PythonRelated Post:
>> How to Setup SFTP Server on Ubuntu – 9 Steps
>> Encoding and Decoding Using Base64 Strings in Python
Conclusion: How to Work with TAR File in Python
Now that you know how to interact with tar files in Python, you are prepared! Tarfile modules in Python facilitate the process of reading, extracting, or even adding files to tar archives. Tarfile is a powerful tool that may be used to construct your tar archives, extract files, and access a file’s content. This article covers it.
So next time when you want to play with a TAR file, remember, that Python has the perfect solution to unlock your treasure.
Happy Coding!