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 an 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 versions 2.7 and 3.3 to 3.7.

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.5.tar.gz (4.7 kB view details)

Uploaded Source

Built Distributions

xopen-0.3.5-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

xopen-0.3.5-py2-none-any.whl (6.8 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for xopen-0.3.5.tar.gz
Algorithm Hash digest
SHA256 3a418d5d3eacc6645d8f002635308651bb6e47b9cabb19ae3abad600aa117ce3
MD5 1e1f718a6959a0a83fc3f49aec86ce37
BLAKE2b-256 e67d15cfc41aa384e6a1dc60852d4df863524c647d852d62190fa638ba9abb58

See more details on using hashes here.

File details

Details for the file xopen-0.3.5-py3-none-any.whl.

File metadata

File hashes

Hashes for xopen-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cf9e004a462419e329bc1df113c91d1a63e2a354b99b6285d11b40b03b911ff0
MD5 1593d43ff3b2d71530f343d7d5ef9708
BLAKE2b-256 89369453c5bd7bc4d349f1efcb96fe70aa3e21b09de2c55d4c85f7001c1f4bda

See more details on using hashes here.

File details

Details for the file xopen-0.3.5-py2-none-any.whl.

File metadata

File hashes

Hashes for xopen-0.3.5-py2-none-any.whl
Algorithm Hash digest
SHA256 ac8b6c8cc16e8e721153caf1215b09764a65e7fb99f93c58a423a8ae59faf61a
MD5 dbe1bee913191d1cca837997976aedf5
BLAKE2b-256 45a519aef5ea2fccf49ade0b0ebd14f9edf31403479efee59af616d8a6ae7bce

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