Read compressed stdin
In python 3.3+, you can pass a file object to gzip.open
:
The filename argument can be an actual filename (a str or bytes object), or an existing file object to read from or write to.
So your code should work if you just omit the fileobj=
:
with gzip.open(sys.stdin, mode='rt') as f:
Or, a slightly more efficient solution:
with gzip.open(sys.stdin.buffer, mode='rb') as f:
If for some odd reason you're using a python older than 3.3, you can directly invoke the gzip.GzipFile
constructor. However, these old versions of the gzip
module didn't have support for files opened in text mode, so we'll use sys.stdin
's underlying buffer instead:
with gzip.GzipFile(fileobj=sys.stdin.buffer) as f: