1

I have a zip file in a directory. I want to read out the contents of the zipfile and after this move its content to other directories to auto sort my zipped files. thing is, the name of these zipfiles will change everytime. I learned about glob being able to handle the asterix for this normally, but it doesn't work with the zipfile import.

I tried:

from zipfile import ZipFile
from glob import glob

path_to_zip = glob(r"C:\me\Python\map_dumper\*.zip")

with ZipFile(path_to_zip, "r") as read_zip:
   ZipFile.namelist()

This gives me an AttributeError:'list' object has no attribute 'seek'

anyone know of another solution for this?

  • 1
    `glob()` returns a list of filenames and `ZipFile` only accepts a single filename. Iterate over the list. – jordanm Dec 30 '22 at 15:57

1 Answers1

2

glob.glob gives you list of filenames compliant with what you specify, for example

glob(r"C:\me\Python\map_dumper\*.zip")

is list of all zip files inside map_dumper catalog, you might use for loop to apply your action to every such file following way

from zipfile import ZipFile
from glob import glob

files_list = glob(r"C:\me\Python\map_dumper\*.zip")

for path_to_zip in files_list:
    with ZipFile(path_to_zip, "r") as read_zip:
        ZipFile.namelist()
Daweo
  • 31,313
  • 3
  • 12
  • 25