Pure python 7-zip library providing comprehensive 7z archive format support with compression, decompression, encryption and CLI tools
—
py7zr provides comprehensive command-line tools for working with 7z archives, supporting all major archive operations including create, extract, list, test, and info commands. The CLI supports password protection, multi-volume archives, and various compression options.
Main entry point for command-line operations.
def main():
"""
Main CLI entry point function.
Parses command line arguments and executes appropriate archive operations.
Called when running 'py7zr' command or 'python -m py7zr'.
"""The py7zr CLI supports the following commands:
Create new 7z archives from files and directories.
# Create archive from directory
py7zr c archive.7z /path/to/directory
# Create archive from multiple files
py7zr c archive.7z file1.txt file2.txt directory/
# Create archive with password
py7zr c -P archive.7z /path/to/directory
password?: ****
# Create multi-volume archive (500KB volumes)
py7zr c -v 500k archive.7z /path/to/large_directory
# Create archive with compression level
py7zr c -l 9 archive.7z /path/to/directory
# Create archive excluding files
py7zr c -x '*.tmp' archive.7z /path/to/directoryOptions:
-P: Prompt for password to encrypt archive-v SIZE: Create multi-volume archive with specified volume size (k/m/g suffixes)-l LEVEL: Set compression level (0-9, default varies by algorithm)-x PATTERN: Exclude files matching patternExtract files from 7z archives with full directory paths.
# Extract entire archive to current directory
py7zr x archive.7z
# Extract to specific directory
py7zr x archive.7z -o /tmp/extracted
# Extract password-protected archive
py7zr x -P archive.7z
password?: ****
# Extract specific files only
py7zr x archive.7z file1.txt directory/file2.txt
# Extract with overwrite confirmation
py7zr x -y archive.7z
# Extract without creating subdirectories (flatten)
py7zr x -j archive.7zOptions:
-o PATH: Extract to specified output directory-P: Prompt for password for encrypted archives-y: Assume yes to all prompts (overwrite existing files)-j: Extract without directory structure (junk paths)Display contents and information about files in 7z archives.
# List all files in archive
py7zr l archive.7z
# List with detailed information
py7zr l -v archive.7z
# List specific files/patterns
py7zr l archive.7z '*.txt'
# List password-protected archive
py7zr l -P archive.7z
password?: ****Options:
-v: Verbose listing with detailed file information-P: Prompt for password for encrypted archivesOutput Format:
Date Time Attr Size Compressed Name
--------- ----- ---------- ------- ---------- --------
2023-01-15 10:30 .....A.... 1024 512 readme.txt
2023-01-15 10:31 D.....A.... 0 0 docs/
2023-01-15 10:31 .....A.... 2048 1024 docs/manual.pdf
--------- ----- ---------- ------- ---------- --------
3072 1536 3 files, 1 foldersVerify integrity of 7z archives by testing decompression without extracting files.
# Test entire archive
py7zr t archive.7z
# Test password-protected archive
py7zr t -P archive.7z
password?: ****
# Test specific files
py7zr t archive.7z file1.txt directory/Options:
-P: Prompt for password for encrypted archivesOutput:
Testing archive: archive.7z
Extracting readme.txt OK
Extracting docs/manual.pdf OK
Everything is Ok
Archives: 1
Files: 2
Size: 3072
Compressed: 1536Display information about supported compression methods, filters, and formats.
# Show all supported formats and methods
py7zr i
# Show version information
py7zr --versionOutput Example:
7-Zip [64] 21.07 : py7zr [Python] 0.20.0
Formats:
C 7z
Methods:
C LZMA2 LZMA BZip2 Deflate Copy ZStandard Brotli PPMd
E 7zAES
Filters:
C Delta BCJ(x86) BCJ(ARM) BCJ(ARMT) BCJ(PPC) BCJ(SPARC) BCJ(IA64)Add files to existing archives (creates new archive if doesn't exist).
# Append files to existing archive
py7zr a archive.7z new_file.txt new_directory/
# Append with password (for encrypted archives)
py7zr a -P archive.7z additional_files/
password?: ****Options:
-P: Prompt for password for encrypted archivesOptions that work with multiple commands:
--help: Show help message and exit--version: Show version information and exit-v: Verbose output (command-specific behavior)-P: Prompt for password-o PATH: Output directory (for extraction)-y: Assume yes to all prompts# Create backup archive
py7zr c -l 6 backup_$(date +%Y%m%d).7z ~/Documents ~/Pictures
# Create encrypted backup
py7zr c -P secure_backup.7z ~/sensitive_data
password?: my_secure_password
# Extract and verify
py7zr t backup.7z
py7zr x backup.7z -o ~/restored_backup
# Multi-volume archive for large datasets
py7zr c -v 1g large_dataset.7z ~/big_data_directory
# List and selective extraction
py7zr l archive.7z
py7zr x archive.7z 'docs/*.pdf' 'config/*.json'#!/bin/bash
# Backup script example
BACKUP_DIR="/backups"
SOURCE_DIR="/home/user/important_data"
DATE=$(date +%Y%m%d_%H%M%S)
ARCHIVE="$BACKUP_DIR/backup_$DATE.7z"
# Create compressed backup
py7zr c -l 9 "$ARCHIVE" "$SOURCE_DIR"
# Verify backup integrity
if py7zr t "$ARCHIVE"; then
echo "Backup created successfully: $ARCHIVE"
else
echo "Backup verification failed!"
exit 1
fi
# Clean old backups (keep last 7 days)
find "$BACKUP_DIR" -name "backup_*.7z" -mtime +7 -deleteThe CLI returns appropriate exit codes:
Common error scenarios:
# Handle password-protected archives
py7zr x secure.7z || echo "Extraction failed - check password"
# Check if archive exists before operations
if py7zr t archive.7z 2>/dev/null; then
py7zr x archive.7z
else
echo "Archive is corrupted or doesn't exist"
fipy7zr CLI respects the following environment variables:
PY7ZR_PASSWORD: Default password for encrypted archives (use with caution)TMPDIR: Directory for temporary files during operations-l 1 for speed, -l 9 for compression)-v) can improve handling of very large datasetsInstall with Tessl CLI
npx tessl i tessl/pypi-py7zr