我想在.txt,.mp3和mp4文件的每个文件中放置一个文件。
import zipfile
import os
path = "G:"
file_zip = zipfile.ZipFile(path+'\\archive.zip', 'w')
for folder, subfolders, files in os.walk(path):
for file in files:
if file.endswith('.jpg,.txt,.mp3,.mp4'):
file_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), path), compress_type = zipfile.ZIP_DEFLATED)
file_zip.close()
当我打开zip文件时,zip文件为空!
答案 0 :(得分:0)
尝试更改:
if file.endswith('.jpg,.txt,.mp3,.mp4'):
为:
if file.endswith(('.jpg', '.txt', '.mp3', '.mp4')):