'python zipfile error TypeError: __init__() missing 1 required positional argument: 'zipinfo'

I'm trying to zip files in a directory having certain extensions.

path_to_data = askdirectory()
os.chdir(path_to_data)

#file_list = [os.path.basename(x) for x in glob.glob("*.png") + glob.glob("ecotaxa*.txt")]
file_list = glob.glob("*.png") + glob.glob("*.txt")

print(file_list)
with zipfile.ZipExtFile(os.path.join(path_to_data, "archaive.zip"), "w") as zipMe:
    for f in file_list:
        zipMe.write(f, compress_type = zipfile.ZIP_DEFLATED)

However, an error was returned:

TypeError: __init__() missing 1 required positional argument: 'zipinfo'

I have no idea what's the main problem here.

I am using Mac, Big Sur ver. 11.4 and Python ver. 3.8.9



Solution 1:[1]

I think you are using wrong class here. ZipExtFile is a File-like object for reading an archive member and Is returned by ZipFile.open(). So it's a class representing an extracted file.

To zip the files use ZipFile class instead.

with zipfile.ZipFile(os.path.join(path_to_data, "archaive.zip"), "w") as zipMe:
             ^^^^^^^

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