I’ve a zip file say ‘test.zip’ and files name as ‘first.txt’ ,’first1.txt’ and ‘second.txt’ . I need to extract only files that start with word ‘first’ . How to do this in python ?
Asked
Active
Viewed 1,058 times
0
-
something similiar: https://stackoverflow.com/questions/56786321/read-multiple-csv-files-zipped-in-one-file – PV8 Aug 19 '19 at 12:58
-
The solution in the question marked as duplicate can be adapted for files. – Paolo Aug 19 '19 at 13:07
1 Answers
1
You can iterate over all files in zip archive and check filename before extracting:
import zipfile
with zipfile.ZipFile('test.zip', 'r') as zp:
files = zipfile.ZipFile.infolist(zp)
for file in files:
if file.filename.startswith('first'):
with open(file.filename, 'wb') as f:
f.write(zp.read(file.filename))

Alderven
- 7,569
- 5
- 26
- 38