Monday, April 21, 2025

Extract Files with Specific Extension from ZIP File

import zipfile

target = 'OLD-BACKUP.zip'

handle = zipfile.ZipFile(target)

for x in handle.namelist():
    if x.endswith('.cat'):
        handle.extract(x, 'UNPACK-SPECIFIC')

This Python code uses the zipfile module to extract specific files from a ZIP archive called 'OLD-BACKUP.zip' that have the file extension .cat to a specific directory 'UNPACK-SPECIFIC'.

The zipfile.ZipFile method is used to create a handle to the specified ZIP file. The namelist method is then called on the handle to get a list of all the filenames in the ZIP archive. A for loop is then used to iterate through each filename in the list.

For each filename, an if statement checks if the filename ends with the string '.cat'. If it does, the extract method is called on the handle to extract that specific file to the directory 'UNPACK-SPECIFIC'.

Note that if the target directory already contains a file with the same name as the file being extracted, it will be overwritten.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...