Skip to main content

Open compressed files transparently

Project description

https://travis-ci.org/marcelm/xopen.svg?branch=master https://img.shields.io/pypi/v/xopen.svg?branch=master

xopen

This small Python module provides a xopen function that works like the built-in open function, but can also deal with compressed files. Supported compression formats are gzip, bzip2 and xz. They are automatically recognized by their file extensions .gz, .bz2 or .xz.

The focus is on being as efficient as possible on all supported Python versions. For example, simply using gzip.open is very slow in older Pythons, and it is a lot faster to use a gzip subprocess. For writing to gzip files, xopen uses pigz when available.

This module has originally been developed as part of the cutadapt tool that is used in bioinformatics to manipulate sequencing data. It has been in successful use within that software for a few years.

xopen is compatible with Python 2.6, 2.7, 3.3, 3.4, 3.5 and 3.6.

Usage

Open a file for reading:

from xopen import xopen

with xopen('file.txt.xz') as f:
    content = f.read()

Or without context manager:

from xopen import xopen

f = xopen('file.txt.xz')
content = f.read()
f.close()

Open a file for writing:

from xopen import xopen

with xopen('file.txt.gz', mode='w') as f:
    f.write('Hello')

Credits

The name xopen was taken from the C function of the same name in the utils.h file which is part of BWA.

Kyle Beauchamp <https://github.com/kyleabeauchamp/> has contributed support for appending to files.

Some ideas were taken from the canopener project. If you also want to open S3 files, you may want to use that module instead.

Author

Marcel Martin <mail@marcelm.net> (@marcelm_ on Twitter)

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

xopen-0.3.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

xopen-0.3.0-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file xopen-0.3.0.tar.gz.

File metadata

  • Download URL: xopen-0.3.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xopen-0.3.0.tar.gz
Algorithm Hash digest
SHA256 629a61f47229647a1daccf9b2cd5a46a01eb55458ff77a6663622b1eaa84bf6f
MD5 87e7bf009ff52c6ed2cce5aa5d849002
BLAKE2b-256 5cf91d7643c22d80a986f72437cc7c27f357cae86789197b340994fb3d68b0c7

See more details on using hashes here.

File details

Details for the file xopen-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for xopen-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ceafade945d4fabe06c85749f7a0a7c4c91765400aa0d85ef87f974126b29198
MD5 4c67ca8a75bdc9b73ff4d0e1c5b33c9f
BLAKE2b-256 51dea18953d6d1169da2a10d6d06353b92a6e689e8dc488e464ada73d1539e9f

See more details on using hashes here.

Supported by

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