python zipfile 把整个文件夹内的文件打包 没有生成.zip文件
发布网友
发布时间:2022-05-05 17:13
我来回答
共2个回答
热心网友
时间:2022-06-27 20:02
- 如果操作系统为 Windows 7 请先 "以管理员身份运行" cmd.exe
cmd.exe 在 C:\Windows\System32 文件夹下。
#! /usr/bin/python
#coding: utf-8
import os
import sys
import zipfile
path = 'e:\\pythonWorkSpace\\practice'
zipfilename = 'e:\\version.zip'
# path is a direactory or not.
if not os.path.isdir(path):
print path + ' No such a direactory'
exit()
if os.path.exists(zipfilename):
# zipfilename is exist.Append.
print 'Add files into ' + zipfilename
zipfp = zipfile.ZipFile(zipfilename, 'a' ,zipfile.ZIP_DEFLATED)
for dirpath, dirnames, filenames in os.walk(path, True):
for filaname in filenames:
direactory = os.path.join(dirpath,filaname)
print 'Add... ' + direactory
zipfp.write(direactory)
else:
# zipfilename is not exist.Create.
print 'Create new file ' + zipfilename
zipfp = zipfile.ZipFile(zipfilename, 'w' ,zipfile.ZIP_DEFLATED)
for dirpath, dirnames, filenames in os.walk(path, True):
for filaname in filenames:
direactory = os.path.join(dirpath,filaname)
print 'Compress... ' + direactory
zipfp.write(direactory)
# Flush and Close zipfilename at last.
zipfp.close()
热心网友
时间:2022-06-27 20:03
亲测 有…可能你没找对地方…