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

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for xopen-0.3.4.tar.gz
Algorithm Hash digest
SHA256 a12395c8925887337e90d26405a7ac4c1bfcdb9c3685cdefece338bf9b80d1b1
MD5 6a0eff0cbcb7322f9bf28c9e53ee628b
BLAKE2b-256 eaa9c2d4a7f28f47647b60d0fff515be09fdc2a94f240275babb703a2d9801ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for xopen-0.3.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2e80463c3e2589885135d5c2504e3e06f60acf5d22ea78580c261bffda281bb2
MD5 d9dec9c65ce2a4c8bfb8c888f8153fb8
BLAKE2b-256 a0c682b2e16067759e71a13086b14d5b412117196c1245206aa3f8dfc24d4422

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