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.

Answer from ghostdog74 on Stack Overflow
🌐
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.
🌐
Real Python
realpython.com › ref › stdlib › tarfile
tarfile | Python Standard Library – Real Python
Language: Python · >>> import tarfile >>> with tarfile.open("sample.tar.gz", mode="r:gz") as tar_file: ... tar_file.getnames() ... ['file1.txt', 'file2.txt'] Supports reading and writing of tar archives · Handles both compressed (gzip, bzip2, lzma, zstd) and uncompressed tar files ·
🌐
AskPython
askpython.com › home › the tarfile module – how to work with tar files in python?
The tarfile Module - How to work with tar files in Python? - AskPython
February 16, 2023 - The following code is an implementation for creating a tar file in Python. Here we use open() method for creating tar file and add() method for adding other files to a tar file. #import module import tarfile #declare filename filename= ...
🌐
TestDriven.io
testdriven.io › tips › f2667a31-8c6c-46d3-91ce-9d7e4bd5b84d
Tips and Tricks - How do I extract a tar file in Python? | TestDriven.io
Python tip: You can extract a tar archive to a destination folder using tarfile. An example👇 · import tarfile with tarfile.open("example.tar.gz") as tar: tar.extractall("destination_folder") View All Tips · Feedback · × ·
🌐
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 »
Find elsewhere
🌐
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.
🌐
LinuxConfig
linuxconfig.org › home › how to create and manipulate tar archives using python
Use Tarfile Module in Python: A Complete Guide
June 3, 2020 - The tarfile module is included in the python standard library, so we don’t need to install it separately; to use it, we just need to “import” it. The recommended way to access a tarball using this module is by the open function; in its ...
🌐
Coderz Column
coderzcolumn.com › tutorials › python › tarfile-simple-guide-to-work-with-tape-archives-in-python
tarfile - Simple Guide to Work with Tape Archives in Python by Sunny Solanki
October 4, 2022 - Below, we have listed signatures of important methods that can be referred to properly using various methods. We have included method definitions in all of our examples. open(name=None,mode='r') - This method takes as input tar file name and ...
🌐
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.
🌐
David Hamann
davidhamann.de › posts › python tarfile directory traversal
Python tarfile directory traversal | David Hamann
September 23, 2022 - import tarfile with open('test.txt', 'w') as f: f.write('Hello') with tarfile.open('my_archive.tar', 'w:xz') as tar: tar.add('test.txt', arcname='../test.txt') Next, give that file to a Python application that extracts tar files, for example:
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-uncompress-a-tar-gz-file-using-python
How to uncompress a ".tar.gz" file using Python ? - GeeksforGeeks
July 23, 2025 - Explanation: This code opens the gfg.tar.gz archive using the tarfile module, prints the list of all files inside the archive, extracts all its contents into the Destination_FolderName folder creating it if it doesn't exist and then closes the archive.
🌐
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.
🌐
Runebook.dev
runebook.dev › en › docs › python › library › tarfile › tarfile.TarFile.open
Troubleshooting Python's tarfile: Solutions for open() Errors and File-Like Object Methods
October 23, 2025 - Instead of letting tarfile.open() handle the opening of the file, you can explicitly open the file yourself using standard Python open() and then pass the file object to the tarfile.TarFile() constructor.