python 压缩整个文件夹代码 zipfile - xd21303/Notebook GitHub Wiki
import zipfile
def zip_folder(zipfilename, dirname): # zipfilename是压缩包名字,dirname是要打包的目录
pre_len = len(os.path.dirname(dirname))
if os.path.isfile(dirname):
with zipfile.ZipFile(zipfilename, 'w') as z:
z.write(dirname)
else:
with zipfile.ZipFile(zipfilename, 'w') as z:
for root, dirs, files in os.walk(dirname):
for single_file in files:
if single_file != zipfilename:
filepath = os.path.join(root, single_file)
arcname = filepath[pre_len:].strip(os.path.sep)
z.write(filepath,arcname)