0

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"

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
toolost
  • 25
  • 4
  • Not sure if I got you right, but `filelist` is not a string, it's, well, a `list`, so you can get the first element and store it in a variable by indexing: `pdfname = filelist[0]`. – fsimonjetz Dec 20 '22 at 12:14

1 Answers1

4

the easiest way:

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