you can use getmembers()
>>> import tarfile
>>> tar = tarfile.open("test.tar")
>>> tar.getmembers()
After that, you can use extractfile() to extract the members as file object. Just an example
import tarfile,os
import sys
os.chdir("/tmp/foo")
tar = tarfile.open("test.tar")
for member in tar.getmembers():
f=tar.extractfile(member)
content=f.read()
print "%s has %d newlines" %(member, content.count("\n"))
print "%s has %d spaces" % (member,content.count(" "))
print "%s has %d characters" % (member, len(content))
sys.exit()
tar.close()
With the file object f in the above example, you can use read(), readlines() etc.
Python
docs.python.org › 3 › library › tarfile.html
tarfile — Read and write tar archive files
For modes 'w:zst', 'x:zst' and 'w|zst', tarfile.open() accepts the keyword argument level to specify the compression level of the file. The keyword argument options may also be passed, providing advanced Zstandard compression parameters described by CompressionParameter.
University of New Brunswick
cs.unb.ca › ~bremner › teaching › cs2613 › books › python3-doc › library › tarfile.html
tarfile — Read and write tar archive files — Python 3.9.2 documentation
If fileobj is specified, it is used as an alternative to a file object opened in binary mode for name. It is supposed to be at position 0. For modes 'w:gz', 'r:gz', 'w:bz2', 'r:bz2', 'x:gz', 'x:bz2', tarfile.open() accepts the keyword argument compresslevel (default 9) to specify the compression level of the file.
W3Schools
w3schools.com › python › ref_module_tarfile.asp
Python tarfile Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... import tarfile import io buffer = io.BytesIO() with tarfile.open(fileobj=buffer, mode='w') as tar: print('Tar archive created in memory') print(f'Format: {tar.format}') Try it Yourself »
Top answer 1 of 6
160
you can use getmembers()
>>> import tarfile
>>> tar = tarfile.open("test.tar")
>>> tar.getmembers()
After that, you can use extractfile() to extract the members as file object. Just an example
import tarfile,os
import sys
os.chdir("/tmp/foo")
tar = tarfile.open("test.tar")
for member in tar.getmembers():
f=tar.extractfile(member)
content=f.read()
print "%s has %d newlines" %(member, content.count("\n"))
print "%s has %d spaces" % (member,content.count(" "))
print "%s has %d characters" % (member, len(content))
sys.exit()
tar.close()
With the file object f in the above example, you can use read(), readlines() etc.
2 of 6
15
you need to use the tarfile module. Specifically, you use an instance of the class TarFile to access the file, and then access the names with TarFile.getnames()
| getnames(self)
| Return the members of the archive as a list of their names. It has
| the same order as the list returned by getmembers().
If instead you want to read the content, then you use this method
| extractfile(self, member)
| Extract a member from the archive as a file object. `member' may be
| a filename or a TarInfo object. If `member' is a regular file, a
| file-like object is returned. If `member' is a link, a file-like
| object is constructed from the link's target. If `member' is none of
| the above, None is returned.
| The file-like object is read-only and provides the following
| methods: read(), readline(), readlines(), seek() and tell()
TutorialsPoint
tutorialspoint.com › article › read-and-write-tar-archive-files-using-python-tarfile
Read and write tar archive files using Python (tarfile)
import tarfile, glob >>> fp=tarfile.open('file.tar','w') >>> for file in glob.glob('*.*'): fp.add(file) >>> fp.close() Creation and extraction of tar files can be achieved through command line interface. For example ‘lines.txt’ file is added in a tar file by following command executed in command window · C:\python36 >python -m tarfile -c line.tar lines.txt
TutorialsPoint
tutorialspoint.com › article › How-are-files-extracted-from-a-tar-file-using-Python
How are files extracted from a tar file using Python?
May 28, 2025 - The extract() method in Python is used to extract a specific file from a TAR archive. This is useful when you don't want to extract the entire archive but only one particular file.
Python
docs.python.org › 3.1 › library › tarfile.html
12.5. tarfile — Read and write tar archive files — Python v3.1.5 documentation
Class for reading and writing tar archives. Do not use this class directly, better use tarfile.open() instead.
Tutorialspoint
tutorialspoint.com › python › tarfile_module.htm
The tarfile Module
The archives can be constructed with gzip, bz2 and lzma compressions or without any compression at all. The open() function defined in this module is used for writing to tar file or reading from it.
GitHub
github.com › python › cpython › blob › main › Lib › tarfile.py
cpython/Lib/tarfile.py at main · python/cpython
# TarFile class. The open() method is the only one that is needed for · # public use; it is the "super"-constructor and is able to select an · # adequate "sub"-constructor for a particular compression using the mapping · # from OPEN_METH. # # This concept allows one to subclass TarFile without losing the comfort of ·
Author python
Python Module of the Week
pymotw.com › 3 › tarfile
tarfile — Tar Archive Access
August 19, 2016 - $ python3 tarfile_extractfile.py README.txt : The examples for the tarfile module use this file and example.tar as data. ERROR: Did not find notthere.txt in tar archive · To unpack the archive and write the files to the file system, use extract() or extractall() instead. ... import tarfile import os os.mkdir('outdir') with tarfile.open('example.tar', 'r') as t: t.extract('README.txt', 'outdir') print(os.listdir('outdir'))
Python Module of the Week
pymotw.com › 2 › tarfile
tarfile – Tar archive access - Python Module of the Week
February 22, 2009 - import tarfile t = tarfile.open('example.tar', 'r') for filename in [ 'README.txt', 'notthere.txt' ]: try: f = t.extractfile(filename) except KeyError: print 'ERROR: Did not find %s in tar archive' % filename else: print filename, ':', f.read() $ python tarfile_extractfile.py README.txt : The examples for the tarfile module use this file and example.tar as data.