
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to extract all the .txt files from a zip file using Python?
In order to extract all .txt files from a zip, you'll need to loop over all files in the zipfile, check if a file is txt file or not. If it is a txt file then extract it. For this we'll use the zipfile module and its extract function.
For example
import zipfile my_zip = zipfile.Zipfile('my_zip_file.zip') # Specify your zip file's name here storage_path = '.' for file in my_zip.namelist(): if my_zip.getinfo(file).filename.endswith('.txt'): my_zip.extract(file, storage_path) # extract the file to current folder if it is a text file
Running the above code will open the my_zip_file.zip and extract all txt files from it and store them in the current directory.
- Related Articles
- Python Program to Read and printing all files from a zip file
- How are files added to a zip file using Python?
- Golang program to read and print all files from zip file
- How to create a zip file using Python?
- How to find all files in a directory with extension .txt in Python?
- How to make a txt file and read txt file from internal storage in android?
- Plot data from a .txt file using matplotlib
- How to store list in a txt file and read list from txt file in android?
- How are files extracted from a tar file using Python?
- How to extract file extension using Python?
- Python – How to Extract all the digits from a String
- How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file?
- How to overwrite a line in a .txt file using Java?
- How to Zip / Unzip files or folders using PowerShell?
- How are files added to a tar file using Python?

Advertisements