- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How are files extracted from a tar file using Python?
You can use the tarfile module to read and write tar files. To extract a tar file, you need to first open the file and then use the extract method of the tarfile module.
For example
import tarfile my_tar = tarfile.open('my_tar.tar.gz') my_tar.extractall('./my_folder') # specify which folder to extract to my_tar.close()
This will extract the my_tar.tar.gz file's contents to my_folder.
You can also extract individual files from the tar using extract(filename, path).
For example
import tarfile my_tar = tarfile.open('my_tar.tar.gz') my_tar.extract('hello.txt','./my_folder') my_tar.close()
- Related Articles
- How are files added to a tar file using Python?
- How to create a tar file using Python?
- How are files added to a zip file using Python?
- Read and write tar archive files using Python (tarfile)
- How to extract all the .txt files from a zip file using Python?
- How to merge multiple files into a new file using Python?
- How to concatenate two files into a new file using Python?
- How to spilt a binary file into multiple files using Python?
- How can titles from a webpage be extracted using BeautifulSoup?
- How to save files using a File Chooser in JavaFX?
- Downloading files from web using Python?
- Python Pandas- Create multiple CSV files from existing CSV file
- How to open multiple files using a File Chooser in JavaFX?
- How to read a file from command line using Python?
- The best way to compress and extract files using the tar command on linux

Advertisements