'Python extract zipfile

I want to extract txt files from zip file to Desktop and i got it with the code below but if zip files have file which has text files, how i extract these text files from file to Desktop?

import zipfile


def abc(path_to_zip_file, directory_to_extract_to):

    with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
         zip_ref.extractall(directory_to_extract_to)

path_to_zip_file ="zip file path"
directory_to_extract_to = "Desktop path"
z = zipfile.ZipFile(path_to_zip_file)

abc(path_to_zip_file, directory_to_extract_to)


Solution 1:[1]

import zipfile, os, shutil
myZip = zipfile.ZipFile('visualvm_207.zip')


for file in myZip.filelist:
    if file.filename.endswith('.txt'):
        source = myZip.open(file)
        target = open(os.path.join(directory_to_extract_to, os.path.basename(file.filename)), "wb")
        with source, target:
            shutil.copyfileobj(source, target)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1