我正在尝试将文件压缩到具有某些扩展名的目录中。
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)
然而,返回了一个错误:
TypeError: __init__() missing 1 required positional argument: 'zipinfo'
我不知道这里的主要问题是什么。
我使用的是 Mac,Big Sur 版本。 11.4 和 Python 版本。 3.8.9
答案 0 :(得分:1)
我认为您在这里使用了错误的类。 ZipExtFile
是 File-like object for reading an archive member and Is returned by ZipFile.open()
。所以它是一个表示提取文件的类。
要压缩文件,请改用 ZipFile
类。
with zipfile.ZipFile(os.path.join(path_to_data, "archaive.zip"), "w") as zipMe:
^^^^^^^