A package to provide pathlib like access to zip & tar archives.
Project description
archive-path
A package to provide pathlib like access to zip & tar archives.
Usage
For reading zip (ZipPath
) or tar (TarPath
) files:
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)
For writing files, you should use within a context manager, or directly call the close
method:
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="**/*")
Note that archive formats do not allow to overwrite existing files (they will raise a FileExistsError
).
For performant access to single files:
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")
These methods allow for faster access to files (using less RAM) in archives containing 1000's of files.
This is because, the archive's file index is only read until the path is found (discarding non-matches),
rather than the standard tarfile
/zipfile
approach that is to read the entire index into memory first.
Windows compatibility
Paths within the archives are always read and written as being /
delimited.
This means that the package works on Windows,
but will not be compatible with archives written outside this package with \\
path delimiters.
Development
This package utilises flit as the build engine, and tox for test automation.
To install these development dependencies:
pip install tox
To run the tests:
tox
and with test coverage:
tox -e py37-cov
The easiest way to write tests, is to edit tests/fixtures.md
To run the code formatting and style checks:
tox -e py37-pre-commit
or directly
pip install pre-commit
pre-commit run --all
Publish to PyPi
Either use flit directly:
pip install flit
flit publish
or trigger the GitHub Action job, by creating a release with a tag equal to the version, e.g. v0.1.1
.
Note, this requires generating an API key on PyPi and adding it to the repository Settings/Secrets
, under the name PYPI_KEY
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for archive_path-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f3f9c46d28ae5459732d1db396afe8fe4e4bf00cc4fb6ee73b81c0588df6b34 |
|
MD5 | 730ca359ffebcce86d3458e7564ca265 |
|
BLAKE2b-256 | b4df4d83db4b8458529c5601edd88d3356e98a86923f165d8c925529b7695c1f |