- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 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.
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.
- How are files added to a zip file using Python?
- How to create a zip file using Python?
- Plot data from a .txt file using matplotlib
- How to make a txt file and read txt file from internal storage in android?
- How to find all files in a directory with extension .txt in Python?
- 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?
- How to Zip / Unzip files or folders using PowerShell?
- Python – How to Extract all the digits from a String
- How to overwrite a line in a .txt file using Java?
- Working with zip files in Python
- How to touch all the files recursively using Python?
- How to close all the opened files using Python?
- How are files added to a tar file using Python?