API documentation#
Tip
Avoid having in-function-signature type annotations with autodoc, by setting the following options:
# -- Options for autodoc ----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#configuration
# Automatically extract typehints when specified and place them in
# descriptions of the relevant function/method.
autodoc_typehints = "description"
# Don't show class signature with the class' name.
autodoc_class_signature = "separated"
Functions that read and write gzipped files.
The user of the file doesn’t have to worry about the compression, but random access is not allowed.
- exception gzip.BadGzipFile#
Exception raised in some cases for invalid gzip files.
- class gzip.GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)#
The GzipFile class simulates most of the methods of a file object with the exception of the truncate() method.
This class only supports opening files in binary mode. If you need to open a compressed file in text mode, use the gzip.open() function.
- close()#
Flush and close the IO object.
This method has no effect if the file is already closed.
- fileno()#
Invoke the underlying file object’s fileno() method.
This will raise AttributeError if the underlying file object doesn’t support fileno().
- flush(zlib_mode=2)#
Flush write buffers, if applicable.
This is not implemented for read-only and non-blocking streams.
- property mtime#
Last modification time read from stream, or None
- read(size=-1)#
Read and return up to n bytes.
If the size argument is omitted, None, or negative, read and return all data until EOF.
If the size argument is positive, and the underlying raw stream is not ‘interactive’, multiple raw reads may be issued to satisfy the byte count (unless EOF is reached first). However, for interactive raw streams (as well as sockets and pipes), at most one raw read will be issued, and a short result does not imply that EOF is imminent.
Return an empty bytes object on EOF.
Return None if the underlying raw stream was open in non-blocking mode and no data is available at the moment.
- read1(size=-1)#
Implements BufferedIOBase.read1()
Reads up to a buffer’s worth of data if size is negative.
- readable()#
Return whether object was opened for reading.
If False, read() will raise OSError.
- readline(size=-1)#
Read and return a line from the stream.
If size is specified, at most size bytes will be read.
The line terminator is always b’n’ for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.
- rewind()#
Return the uncompressed stream file position indicator to the beginning of the file
- seek(offset, whence=0)#
Change the stream position to the given byte offset.
- offset
The stream position, relative to ‘whence’.
- whence
The relative position to seek from.
The offset is interpreted relative to the position indicated by whence. Values for whence are:
os.SEEK_SET or 0 – start of stream (the default); offset should be zero or positive
os.SEEK_CUR or 1 – current stream position; offset may be negative
os.SEEK_END or 2 – end of stream; offset is usually negative
Return the new absolute position.
- seekable()#
Return whether object supports random access.
If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek().
- tell()#
Return current stream position.
- writable()#
Return whether object was opened for writing.
If False, write() will raise OSError.
- write(data)#
Write buffer b to the IO stream.
Return the number of bytes written, which is always the length of b in bytes.
Raise BlockingIOError if the buffer is full and the underlying raw stream cannot accept more data at the moment.
- gzip.compress(data, compresslevel=9, *, mtime=None)#
Compress data in one shot and return the compressed string.
compresslevel sets the compression level in range of 0-9. mtime can be used to set the modification time. The modification time is set to the current time by default.
- gzip.decompress(data)#
Decompress a gzip compressed string in one shot. Return the decompressed string.
- gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)#
Open a gzip-compressed file in binary or text mode.
The filename argument can be an actual filename (a str or bytes object), or an existing file object to read from or write to.
The mode argument can be “r”, “rb”, “w”, “wb”, “x”, “xb”, “a” or “ab” for binary mode, or “rt”, “wt”, “xt” or “at” for text mode. The default mode is “rb”, and the default compresslevel is 9.
For binary mode, this function is equivalent to the GzipFile constructor: GzipFile(filename, mode, compresslevel). In this case, the encoding, errors and newline arguments must not be provided.
For text mode, a GzipFile object is created, and wrapped in an io.TextIOWrapper instance with the specified encoding, error handling behavior, and line ending(s).