Skip to main content

A generic context manager for readable files.

Project description

Provides the gopen.readable() and gopen.gread() functions.

readable(source) supports with statements for readable file objects:

>>> with gopen.readable(source) as f:
>>>     <read from f>

gread(source) returns an iterator over input lines:

>>> from gopen import gread
>>> lines = gread(source)
>>> lines
<generator object gread at 0x7f45752ebf10>

<source> can be:

  • a readable file object,
  • a file descriptor and
  • a file pathname.

gzip and bzip2-compressed files will be decompressed on the fly. No side effects: if source is a file descriptor or a file handle, it will not be closed.

Example:

>>> f = open('txt', 'r')
>>> for source in ['txt', 'txt.gz', 'txt.bz2', f]:
...     print('%r (%r)' % (list(gread(source)), source))
...
['Simple is\n', 'better than complex.'] ('txt')
['Simple is\n', 'better than complex.'] ('txt.gz')
['Simple is\n', 'better than complex.'] ('txt.bz2')
['Simple is\n', 'better than complex.'] (<_io.TextIOWrapper name='txt' mode='r' encoding='UTF-8'>)
>>> f.closed
False

Changes

0.6 (2019-05-27)

  • Python3 only (>= 3.4)

Project details


Download files

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

Files for gopen, version 0.6.4
Filename, size File type Python version Upload date Hashes
Filename, size gopen-0.6.4-py3-none-any.whl (3.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size gopen-0.6.4.tar.gz (4.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page