我在UBUNTU 16.04中起诉python 2.7 我的代码:
def zipdir(path, zip_file_name ):
# ziph is zipfile handle
ziph = zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
ziph.close()
创建包含所有内部目录的dir zip,例如,如果我想对/home/ubuntu/folder
进行zip压缩,它将对/home/ubutnu/folder
进行压缩,而不仅仅是文件夹和home里面只有ubuntu,而Ubuntu里面只有文件夹。如何只为文件夹创建zip?
答案 0 :(得分:1)
尝试使用arcname参数。
例如:
def zipdir(path, zip_file_name ):
# ziph is zipfile handle
ziph = zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file), arcname=file)
ziph.close()