跳转到主要内容

提供类似于pathlib对zip & tar归档访问的包。

项目描述

archive-path

Build Status codecov.io PyPI version Conda Version

提供类似于pathlib对zip & tar归档访问的包。

安装

$ pip install archive-path

用法

用于读取zip (ZipPath) 或tar (TarPath) 文件

from archive_path import TarPath, ZipPath

path = TarPath("path/to/file.tar.gz", mode="r:gz")

sub_path = path / "folder" / "file.txt"
assert sub_path.filepath == "path/to/file.tar.gz"
assert sub_path.at == "folder/file.txt"
assert sub_path.exists() and sub_path.is_file()
assert sub_path.parent.is_dir()
content = sub_path.read_text()

for sub_path in path.iterdir():
    print(sub_path)

对于写入文件,您应该在上下文中使用,或直接调用close方法

with TarPath("path/to/file.tar.gz", mode="w:gz") as path:

    (path / "new_file.txt").write_text("hallo world")
    # there are also some features equivalent to shutil
    (path / "other_file.txt").putfile("path/to/external_file.txt")
    (path / "other_folder").puttree("path/to/external_folder", pattern="**/*")

请注意,归档格式不允许覆盖现有文件(将引发FileExistsError)。

用于对单个文件进行高性能访问

from archive_path import read_file_in_tar, read_file_in_zip

content = read_file_in_tar("path/to/file.tar.gz", "file.txt", encoding="utf8")

这些方法允许更快地访问归档中的文件(使用更少的RAM)。这是因为,归档的文件索引只读取到找到路径为止(丢弃不匹配的项),而不是先读取整个索引到内存中的标准tarfile/zipfile方法。

Windows兼容性

归档内的路径始终以/分隔符进行读取和写入。这意味着该软件包在Windows上可以工作,但与使用\\路径分隔符在此包外写入的归档不兼容。

开发

有关如何为此软件包做出贡献的详细信息,请参阅CONTRIBUTING.md

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

archive-path-0.4.2.tar.gz (19.4 kB 查看哈希值)

上传时间

构建分布

archive_path-0.4.2-py3-none-any.whl (18.9 kB 查看哈希值)

上传时间 Python 3

由支持