{"id":1640,"library":"py7zr","title":"Py7zr - Pure Python 7-Zip Library","description":"Py7zr is a pure Python library for working with 7-Zip archives, enabling creation, extraction, and inspection of `.7z` files without requiring external binaries. The library is actively maintained with a consistent release cadence, recently moving to version 1.x, and supports modern Python versions.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/miurahr/py7zr","tags":["archive","compression","7zip","pure-python"],"install":[{"cmd":"pip install py7zr","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"SevenZipFile","correct":"from py7zr import SevenZipFile"},{"symbol":"is_7zfile","correct":"from py7zr import is_7zfile"}],"quickstart":{"code":"import py7zr\nimport os\n\narchive_name = \"example.7z\"\nfile_to_archive = \"test_file.txt\"\n\n# Create a dummy file for archiving\nwith open(file_to_archive, \"w\") as f:\n    f.write(\"This is a test file for py7zr.\\n\")\n\ntry:\n    # Create a 7z archive\n    with py7zr.SevenZipFile(archive_name, 'w') as archive:\n        archive.write(file_to_archive, arcname='renamed_test_file.txt')\n    print(f\"Archive '{archive_name}' created with '{file_to_archive}'.\")\n\n    # Extract from the 7z archive\n    extract_dir = \"extracted_files\"\n    os.makedirs(extract_dir, exist_ok=True)\n\n    with py7zr.SevenZipFile(archive_name, mode='r') as archive:\n        archive.extractall(path=extract_dir)\n    print(f\"Files extracted to '{extract_dir}'.\")\n\n    # Verify extraction\n    extracted_path = os.path.join(extract_dir, 'renamed_test_file.txt')\n    if os.path.exists(extracted_path):\n        with open(extracted_path, 'r') as f:\n            content = f.read()\n            print(f\"Content of extracted file: {content.strip()}\")\n    else:\n        print(\"Extraction verification failed.\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    # Clean up created files and directories\n    if os.path.exists(file_to_archive):\n        os.remove(file_to_archive)\n    if os.path.exists(archive_name):\n        os.remove(archive_name)\n    if os.path.exists(extract_dir):\n        for root, dirs, files in os.walk(extract_dir, topdown=False):\n            for name in files:\n                os.remove(os.path.join(root, name))\n            for name in dirs:\n                os.rmdir(os.path.join(root, name))\n        os.rmdir(extract_dir)\n    print(\"Cleanup complete.\")","lang":"python","description":"This quickstart demonstrates how to create a 7z archive with a single file and then extract its contents to a new directory. It includes basic error handling and cleanup."},"warnings":[{"fix":"Ensure your environment uses Python 3.10+ or pin py7zr to a version below 1.0.0 (e.g., `py7zr<1.0.0`).","message":"Versions of py7zr from 1.0.0 onwards require Python 3.10 or higher. Earlier versions supported Python 3.7+.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Update your code to access file information via attributes of the `FileInfo` object (e.g., `info.name`, `info.size`).","message":"The `getInfo()` method of `SevenZipFile` now returns `FileInfo` dataclass objects instead of older tuple or dictionary structures. Code expecting direct dictionary/tuple access will break.","severity":"breaking","affected_versions":">=1.1.0"},{"fix":"For robust handling of byte streams, upgrade to py7zr version 1.1.0 or newer. If on older versions, ensure file-like objects are properly buffered or use file paths where possible.","message":"Prior to version 1.1.0, using `SevenZipFile` with `IO[bytes]` stream objects for reading or writing archives might have been inconsistent or caused issues. This was explicitly fixed in v1.1.0.","severity":"gotcha","affected_versions":"<1.1.0"},{"fix":"Upgrade to py7zr version 0.21.1 or newer to ensure proper resource management when using `unpack_7zarchive`. Alternatively, use `SevenZipFile` with a `with` statement for explicit context management.","message":"In versions prior to 0.21.1, the `unpack_7zarchive` function might not have reliably closed the archive handle, potentially leading to resource leaks. This was addressed in v0.21.1.","severity":"gotcha","affected_versions":"<0.21.1"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}