packio

Public methods and classes for packio package.

Submodules

Classes

Reader

Context manager to read multiple objects from the same file.

Writer

Context manager to write multiple objects to the same file.

Functions

unzip(→ None)

Unzip an archive into a destination directory.

unzipflat(→ None)

Unzip a file into a destination directory.

zip(→ None)

Zip a directory to create an archive.

zipflat(→ None)

Zip files into a single archive with no directory structure.

Package Contents

class packio.Reader(path: pathlib.Path)[source]

Context manager to read multiple objects from the same file.

Initialize zipfile with a file path.

path[source]
__enter__() Reader[source]

Create temporary directory and zip object, return callable.

file(filename: str) pathlib.Path[source]

Unzip the file to a temporary path and return that path.

__exit__(exc_type: Type[BaseException] | None, exc_value: BaseException | None, traceback: types.TracebackType | None) None[source]

Exit the context manager.

class packio.Writer(path: pathlib.Path)[source]

Context manager to write multiple objects to the same file.

Initialize the reader with a file path.

path[source]
__enter__() ArchiveWriter[source]

Create temporary directory and zip object, return callable.

__exit__(exc_type: Type[BaseException] | None, exc_value: BaseException | None, traceback: types.TracebackType | None) None[source]

Exit the context manager.

packio.unzip(*, file: PathType, dest_dir: PathType) None[source]

Unzip an archive into a destination directory.

Parameters:
  • file – Path to the zip archive.

  • dest_dir – Directory to unzip the archive into.

packio.unzipflat(*, file: PathType, dest_dir: PathType, overwrite: bool = False) None[source]

Unzip a file into a destination directory.

Parameters:
  • file – Path to the zip archive.

  • dest_dir – An existing directory to unzip the archive into.

  • overwrite – If True, overwrite any existing files in the destination directory.

Raises:
  • ValueError – If the input file is not a zip archive.

  • ValueError – If any contents of the input zip archive are directories – expect a flat archive.

  • FileExistsError – If any files in the archive would overwrite existing files in the destination directory.

packio.zip(directory: PathType, *, outfile: PathType, format: str = 'zip') None[source]

Zip a directory to create an archive.

This preserves the directory structure within the zip file. Implemented as a thin wrapper around shutil.make_archive.

Parameters:
  • directory – Directory containing files to zip.

  • outfile – Full path to the resulting zip archive, including any extension. Users may choose not to add an extension (overriding the behavior of shutil.make_archive, which adds an extension).

  • format – Format of the archive, default is ‘zip’. Other formats like ‘tar’, ‘gztar’, etc. can be used.

packio.zipflat(*, files: list[PathType], outfile: PathType) None[source]

Zip files into a single archive with no directory structure.

Parameters:
  • files – List of files to zip.

  • outfile – Path to the resulting zip archive.

Raises:

ValueError – If the names of the provided files are not unique.