Open compressed files transparently
Project description
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.4 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.
Changes
v0.5.0
By default, pigz is now only allowed to use at most four threads. This hopefully reduces problems some users had with too many threads when opening many files at the same time.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file xopen-0.5.0.dev0.tar.gz
.
File metadata
- Download URL: xopen-0.5.0.dev0.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a98f7716f8580d34aedfaca03cab2afe9367ed3e7e6536e9f4bdf5ca7eeb9f12 |
|
MD5 | 2908a08644f6eb8a8ada2a967b5332bd |
|
BLAKE2b-256 | 1d64f85e1e8749cf26f9aca661c1523f0a627d41c47883cec9bd74dea90da589 |