crd*_*vis 827
最简单的方法是使用shutil.make_archive.它支持zip和tar格式.
import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
Run Code Online (Sandbox Code Playgroud)
如果你需要做一些比压缩整个目录更复杂的事情(比如跳过某些文件),那么你需要像其他人建议的那样深入研究zipfile模块.
Mar*_*ers 472
正如其他人指出的那样,你应该使用zipfile.文档告诉您哪些功能可用,但没有真正解释如何使用它们来压缩整个目录.我认为最简单的解释一些示例代码:
#!/usr/bin/env python
import os
import zipfile
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
if __name__ == '__main__':
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
Run Code Online (Sandbox Code Playgroud)
改编自:http://www.devshed.com/c/a/Python/Python-UnZipped/
Ben*_*mes 62
要将内容添加mydirectory到新的zip文件,包括所有文件和子目录:
import os
import zipfile
zf = zipfile.ZipFile("myzipfile.zip", "w")
for dirname, subdirs, files in os.walk("mydirectory"):
zf.write(dirname)
for filename in files:
zf.write(os.path.join(dirname, filename))
zf.close()
Run Code Online (Sandbox Code Playgroud)
Aar*_*all 47
如何在Python中创建目录结构的zip存档?
在Python 2.7+中,shutil有一个make_archive功能.
from shutil import make_archive
make_archive(
'zipfile_name',
'zip', # the archive format - or tar, bztar, gztar
root_dir=None, # root for archive - current working dir if None
base_dir=None) # start archiving from here - cwd if None too
Run Code Online (Sandbox Code Playgroud)
这里将命名压缩存档zipfile_name.zip.如果base_dir距离较远root_dir,它将排除不在base_dir的文件,但仍然将父目录中的文件存档到root_dir.
我确实在Cygwin上用2.7测试了这个问题 - 它想要一个root_dir参数,对于cwd:
make_archive('zipfile_name', 'zip', root_dir='.')
Run Code Online (Sandbox Code Playgroud)
您也可以使用以下zipfile模块从shell执行此操作:
$ python -m zipfile -c zipname sourcedir
Run Code Online (Sandbox Code Playgroud)
zipname您想要的目标文件的名称在哪里(.zip如果需要,可以添加,不会自动添加),sourcedir是目录的路径.
如果你想拉上一个Python包用__init__.py和__main__.py,和你不想要的父目录,它是
$ python -m zipfile -c zipname sourcedir/*
Run Code Online (Sandbox Code Playgroud)
和
$ python zipname
Run Code Online (Sandbox Code Playgroud)
会运行包.(请注意,您无法将子包作为压缩存档的入口点运行.)
如果您有python3.5 +,并且特别想要压缩Python包,请使用zipapp:
$ python -m zipapp myapp
$ python myapp.pyz
Run Code Online (Sandbox Code Playgroud)
Geo*_*lly 31
此函数将递归压缩目录树,压缩文件,并在归档中记录正确的相对文件名.存档条目与生成的条目相同zip -r output.zip source_dir.
import os
import zipfile
def make_zipfile(output_filename, source_dir):
relroot = os.path.abspath(os.path.join(source_dir, os.pardir))
with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as zip:
for root, dirs, files in os.walk(source_dir):
# add directory (needed for empty dirs)
zip.write(root, os.path.relpath(root, relroot))
for file in files:
filename = os.path.join(root, file)
if os.path.isfile(filename): # regular files only
arcname = os.path.join(os.path.relpath(root, relroot), file)
zip.write(filename, arcname)
Run Code Online (Sandbox Code Playgroud)
JD *_*nki 31
使用 python 3.9pathlib和zipfile模块,您可以从系统中的任何位置创建 zip 文件。
def zip_dir(dir: Union[Path, str], filename: Union[Path, str]):
"""Zip the provided directory without navigating to that directory using `pathlib` module"""
# Convert to Path object
dir = Path(dir)
with zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED) as zip_file:
for entry in dir.rglob("*"):
zip_file.write(entry, entry.relative_to(dir))
Run Code Online (Sandbox Code Playgroud)
它很整洁,有打字功能,并且代码较少。
mon*_*ime 16
现代 Python (3.6+) 使用该pathlib模块进行类似 OOP 的简洁路径处理以及pathlib.Path.rglob()递归通配。据我所知,这相当于 George V. Reilly 的答案:压缩 zip,最上面的元素是一个目录,保留空目录,使用相对路径。
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
from os import PathLike
from typing import Union
def zip_dir(zip_name: str, source_dir: Union[str, PathLike]):
src_path = Path(source_dir).expanduser().resolve(strict=True)
with ZipFile(zip_name, 'w', ZIP_DEFLATED) as zf:
for file in src_path.rglob('*'):
zf.write(file, file.relative_to(src_path.parent))
Run Code Online (Sandbox Code Playgroud)
注意:正如可选类型提示所示,zip_name不能是 Path 对象(将在 3.6.2+ 中修复)。
Vad*_*S J 16
使用shutil,它是python标准库集的一部分.使用shutil非常简单(参见下面的代码):
码:
import shutil
shutil.make_archive('/home/user/Desktop/Filename','zip','/home/username/Desktop/Directory')
Run Code Online (Sandbox Code Playgroud)
小智 11
要将压缩添加到生成的zip文件,请查看此链接.
你需要改变:
zip = zipfile.ZipFile('Python.zip', 'w')
Run Code Online (Sandbox Code Playgroud)
至
zip = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
Run Code Online (Sandbox Code Playgroud)
要保留要归档的父目录下的文件夹层次结构:
from pathlib import Path
import zipfile
fp_zip = Path("output.zip")
path_to_archive = Path("./path-to-archive")
with zipfile.ZipFile(fp_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
for fp in path_to_archive.glob("**/*"):
zipf.write(fp, arcname=fp.relative_to(path_to_archive))
Run Code Online (Sandbox Code Playgroud)
我对Mark Byers给出的代码做了一些修改.如果你有它们,下面的函数也会添加空目录.示例应该更清楚地说明添加到zip的路径是什么.
#!/usr/bin/env python
import os
import zipfile
def addDirToZip(zipHandle, path, basePath=""):
"""
Adding directory given by \a path to opened zip file \a zipHandle
@param basePath path that will be removed from \a path when adding to archive
Examples:
# add whole "dir" to "test.zip" (when you open "test.zip" you will see only "dir")
zipHandle = zipfile.ZipFile('test.zip', 'w')
addDirToZip(zipHandle, 'dir')
zipHandle.close()
# add contents of "dir" to "test.zip" (when you open "test.zip" you will see only it's contents)
zipHandle = zipfile.ZipFile('test.zip', 'w')
addDirToZip(zipHandle, 'dir', 'dir')
zipHandle.close()
# add contents of "dir/subdir" to "test.zip" (when you open "test.zip" you will see only contents of "subdir")
zipHandle = zipfile.ZipFile('test.zip', 'w')
addDirToZip(zipHandle, 'dir/subdir', 'dir/subdir')
zipHandle.close()
# add whole "dir/subdir" to "test.zip" (when you open "test.zip" you will see only "subdir")
zipHandle = zipfile.ZipFile('test.zip', 'w')
addDirToZip(zipHandle, 'dir/subdir', 'dir')
zipHandle.close()
# add whole "dir/subdir" with full path to "test.zip" (when you open "test.zip" you will see only "dir" and inside it only "subdir")
zipHandle = zipfile.ZipFile('test.zip', 'w')
addDirToZip(zipHandle, 'dir/subdir')
zipHandle.close()
# add whole "dir" and "otherDir" (with full path) to "test.zip" (when you open "test.zip" you will see only "dir" and "otherDir")
zipHandle = zipfile.ZipFile('test.zip', 'w')
addDirToZip(zipHandle, 'dir')
addDirToZip(zipHandle, 'otherDir')
zipHandle.close()
"""
basePath = basePath.rstrip("\\/") + ""
basePath = basePath.rstrip("\\/")
for root, dirs, files in os.walk(path):
# add dir itself (needed for empty dirs
zipHandle.write(os.path.join(root, "."))
# add files
for file in files:
filePath = os.path.join(root, file)
inZipPath = filePath.replace(basePath, "", 1).lstrip("\\/")
#print filePath + " , " + inZipPath
zipHandle.write(filePath, inZipPath)
Run Code Online (Sandbox Code Playgroud)
以上是一个简单的功能,适用于简单的情况.您可以在我的Gist中找到更优雅的课程:https: //gist.github.com/Eccenux/17526123107ca0ac28e6
| 归档时间: |
|
| 查看次数: |
300978 次 |
| 最近记录: |