Skip to main content

A Python library for reading and writing gzip-compressed text files

Project description

gzip-txt

A lightweight Python library for reading and writing gzip-compressed text files with UTF-8 encoding.

Features

  • Simple API: Two static methods (read and write) for all operations
  • UTF-8 encoding: Automatic handling of UTF-8 encoding for international characters
  • Flexible writing: Supports writing strings or iterables of strings
  • Type safety: Supports both str and Path objects for file paths
  • Error handling: Comprehensive exception hierarchy for precise error handling
  • Zero dependencies: Uses only Python standard library (gzip)

Installation

pip install gzip-txt

Quick Start

Reading a gzip-compressed text file

from gzip_txt import GzipTxt

# Read a gzip-compressed text file
content = GzipTxt.read("data.txt.gz")

# content is a string
print(content)

Writing to a gzip-compressed text file

from gzip_txt import GzipTxt

# Write a string to a gzip-compressed text file
GzipTxt.write("output.txt.gz", "Hello, World!")

# Write multiple lines from an iterable
lines = ["Line 1", "Line 2", "Line 3"]
GzipTxt.write("output.txt.gz", lines)

Using Path objects

from pathlib import Path
from gzip_txt import GzipTxt

# Both str and Path objects are supported
path = Path("data.txt.gz")
content = GzipTxt.read(path)

GzipTxt.write(Path("output.txt.gz"), "Example text")

API Reference

GzipTxt

Main utility class for reading and writing gzip-compressed text files. All methods are static.

GzipTxt.read(path)

Read a gzip-compressed text file.

Parameters:

  • path (str | Path): Path to the gzip-compressed text file.

Returns:

  • str: Text content as a string.

Raises:

  • GzipTxtReadError: If reading fails due to:
    • File not found
    • Invalid gzip format
    • Encoding errors
    • I/O errors

Example:

from gzip_txt import GzipTxt

content = GzipTxt.read("data.txt.gz")

GzipTxt.write(path, content)

Write content to a gzip-compressed text file.

The file is written with UTF-8 encoding. If content is a string, it is written as-is. If content is an iterable of strings, each string is written as a line (no newline is added automatically).

Parameters:

  • path (str | Path): Path to the output file.
  • content (str | Iterable[str]): Text content to write. Can be:
    • A string: written as-is
    • An iterable of strings: each string is written as a line

Raises:

  • GzipTxtWriteError: If writing fails due to:
    • Permission denied
    • Disk space issues
    • I/O errors
  • TypeError: If content is not a string or iterable of strings.

Example:

from gzip_txt import GzipTxt

# Write a string
GzipTxt.write("output.txt.gz", "Hello, World!")

# Write multiple lines
lines = ["Line 1", "Line 2", "Line 3"]
GzipTxt.write("output.txt.gz", lines)

Exceptions

GzipTxtError

Base exception class for all gzip-txt related errors.

Attributes:

  • message: Primary error message (required)
  • file_path: Path to the file that caused the error (optional)

GzipTxtReadError

Raised when reading a gzip-compressed text file fails.

This exception indicates that an error occurred during the read operation, such as file not found, invalid gzip format, encoding errors, or I/O errors.

Attributes:

  • message: Human-readable error message describing the issue
  • file_path: Path to the file that caused the error (optional)

Example:

from gzip_txt import GzipTxt, GzipTxtReadError

try:
    content = GzipTxt.read("nonexistent.txt.gz")
except GzipTxtReadError as e:
    print(f"Error reading file: {e.message}")
    print(f"File path: {e.file_path}")

GzipTxtWriteError

Raised when writing a gzip-compressed text file fails.

This exception indicates that an error occurred during the write operation, such as permission denied, disk space issues, or I/O errors.

Attributes:

  • message: Human-readable error message describing the issue
  • file_path: Path to the file that caused the error (optional)

Example:

from gzip_txt import GzipTxt, GzipTxtWriteError

try:
    GzipTxt.write("/readonly/output.txt.gz", "Hello, World!")
except GzipTxtWriteError as e:
    print(f"Error writing file: {e.message}")
    print(f"File path: {e.file_path}")

Requirements

  • Python >= 3.10

No external dependencies required. This package uses only Python standard library modules (gzip).

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gzip_txt-1.0.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gzip_txt-1.0.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file gzip_txt-1.0.0.tar.gz.

File metadata

  • Download URL: gzip_txt-1.0.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for gzip_txt-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7c648e434d37b07ebd97bbf491d1265b9f1000319ecdd624c86ee2fdaaf96d6f
MD5 b747e6f0b662a229efc99c3b9b0034d1
BLAKE2b-256 9a5dab83809f34d11e4d6bb01f2347874f0d5296b7ff3c9de5007ed7e038017b

See more details on using hashes here.

File details

Details for the file gzip_txt-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: gzip_txt-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for gzip_txt-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b26a4d502f7224670aa7cfd55f19487ce5dd8bb3b8cd790de0c2885cd958f510
MD5 ae8e2149c38676ef33f7a0700ce3373c
BLAKE2b-256 79ab7bada3160db2ecbbebde123ed84f36b684f88664d3dbc93294d2081cb4c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page