Search code examples
pythonpython-zipfile

Extracting Values From String


I have a string generated from namelist() of ZipFile and I want to save in a variable only the filename of the pdf file:

with ZipFile(zipfile, 'r') as f:
    filelist = f.namelist()
    print(filelist)

The output is: ['test.pdf', 'image_3.png', 'image-1.jpg', 'text-2.txt']

For example I want to store in the variable pdfname the value "test.pdf"


Solution

  • the easiest way:

    with ZipFile(zipfile, 'r') as f:
       filelist = list(filter(lambda x: x.endswith(".pdf"),f.namelist()))
       print(filelist)