{
    "mode": "pydoc",
    "parameter": "gzip",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/pydoc/gzip/json",
    "generated": "2026-06-02T13:22:24Z",
    "sections": {
        "NAME": {
            "content": "gzip - Functions that read and write gzipped files.\n",
            "subsections": []
        },
        "MODULE REFERENCE": {
            "content": "https://docs.python.org/3.10/library/gzip.html\n\nThe following documentation is automatically generated from the Python\nsource files.  It may be incomplete, incorrect or include features that\nare considered implementation detail and may vary between Python\nimplementations.  When in doubt, consult the module reference at the\nlocation listed above.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The user of the file doesn't have to worry about the compression,\nbut random access is not allowed.\n",
            "subsections": []
        },
        "CLASSES": {
            "content": "compression.BaseStream(io.BufferedIOBase)\nGzipFile\nbuiltins.OSError(builtins.Exception)\nBadGzipFile\n",
            "subsections": [
                {
                    "name": "class BadGzipFile",
                    "content": "|  Exception raised in some cases for invalid gzip files.\n|\n|  Method resolution order:\n|      BadGzipFile\n|      builtins.OSError\n|      builtins.Exception\n|      builtins.BaseException\n|      builtins.object\n|\n|  Data descriptors defined here:\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from builtins.OSError:\n|\n|  init(self, /, *args, kwargs)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  reduce(...)\n|      Helper for pickle.\n|\n|  str(self, /)\n|      Return str(self).\n|\n|  ----------------------------------------------------------------------\n|  Static methods inherited from builtins.OSError:\n|\n|  new(*args, kwargs) from builtins.type\n|      Create and return a new object.  See help(type) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from builtins.OSError:\n|\n|  characterswritten\n|\n|  errno\n|      POSIX exception code\n|\n|  filename\n|      exception filename\n|\n|  filename2\n|      second exception filename\n|\n|  strerror\n|      exception strerror\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from builtins.BaseException:\n|\n|  delattr(self, name, /)\n|      Implement delattr(self, name).\n|\n|  getattribute(self, name, /)\n|      Return getattr(self, name).\n|\n|  repr(self, /)\n|      Return repr(self).\n|\n|  setattr(self, name, value, /)\n|      Implement setattr(self, name, value).\n|\n|  setstate(...)\n|\n|  withtraceback(...)\n|      Exception.withtraceback(tb) --\n|      set self.traceback to tb and return self.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from builtins.BaseException:\n|\n|  cause\n|      exception cause\n|\n|  context\n|      exception context\n|\n|  dict\n|\n|  suppresscontext\n|\n|  traceback\n|\n|  args\n"
                },
                {
                    "name": "class GzipFile",
                    "content": "|  GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)\n|\n|  The GzipFile class simulates most of the methods of a file object with\n|  the exception of the truncate() method.\n|\n|  This class only supports opening files in binary mode. If you need to open a\n|  compressed file in text mode, use the gzip.open() function.\n|\n|  Method resolution order:\n|      GzipFile\n|      compression.BaseStream\n|      io.BufferedIOBase\n|      io.BufferedIOBase\n|      io.IOBase\n|      io.IOBase\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  init(self, filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)\n|      Constructor for the GzipFile class.\n|\n|      At least one of fileobj and filename must be given a\n|      non-trivial value.\n|\n|      The new class instance is based on fileobj, which can be a regular\n|      file, an io.BytesIO object, or any other object which simulates a file.\n|      It defaults to None, in which case filename is opened to provide\n|      a file object.\n|\n|      When fileobj is not None, the filename argument is only used to be\n|      included in the gzip file header, which may include the original\n|      filename of the uncompressed file.  It defaults to the filename of\n|      fileobj, if discernible; otherwise, it defaults to the empty string,\n|      and in this case the original filename is not included in the header.\n|\n|      The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or\n|      'xb' depending on whether the file will be read or written.  The default\n|      is the mode of fileobj if discernible; otherwise, the default is 'rb'.\n|      A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and\n|      'wb', 'a' and 'ab', and 'x' and 'xb'.\n|\n|      The compresslevel argument is an integer from 0 to 9 controlling the\n|      level of compression; 1 is fastest and produces the least compression,\n|      and 9 is slowest and produces the most compression. 0 is no compression\n|      at all. The default is 9.\n|\n|      The mtime argument is an optional numeric timestamp to be written\n|      to the last modification time field in the stream when compressing.\n|      If omitted or None, the current time is used.\n|\n|  repr(self)\n|      Return repr(self).\n|\n|  close(self)\n|      Flush and close the IO object.\n|\n|      This method has no effect if the file is already closed.\n|\n|  fileno(self)\n|      Invoke the underlying file object's fileno() method.\n|\n|      This will raise AttributeError if the underlying file object\n|      doesn't support fileno().\n|\n|  flush(self, zlibmode=2)\n|      Flush write buffers, if applicable.\n|\n|      This is not implemented for read-only and non-blocking streams.\n|\n|  peek(self, n)\n|\n|  read(self, size=-1)\n|      Read and return up to n bytes.\n|\n|      If the argument is omitted, None, or negative, reads and\n|      returns all data until EOF.\n|\n|      If the argument is positive, and the underlying raw stream is\n|      not 'interactive', multiple raw reads may be issued to satisfy\n|      the byte count (unless EOF is reached first).  But for\n|      interactive raw streams (as well as sockets and pipes), at most\n|      one raw read will be issued, and a short result does not imply\n|      that EOF is imminent.\n|\n|      Returns an empty bytes object on EOF.\n|\n|      Returns None if the underlying raw stream was open in non-blocking\n|      mode and no data is available at the moment.\n|\n|  read1(self, size=-1)\n|      Implements BufferedIOBase.read1()\n|\n|      Reads up to a buffer's worth of data if size is negative.\n|\n|  readable(self)\n|      Return whether object was opened for reading.\n|\n|      If False, read() will raise OSError.\n|\n|  readline(self, size=-1)\n|      Read and return a line from the stream.\n|\n|      If size is specified, at most size bytes will be read.\n|\n|      The line terminator is always b'\\n' for binary files; for text\n|      files, the newlines argument to open can be used to select the line\n|      terminator(s) recognized.\n|\n|  rewind(self)\n|      Return the uncompressed stream file position indicator to the\n|      beginning of the file\n|\n|  seek(self, offset, whence=0)\n|      Change stream position.\n|\n|      Change the stream position to the given byte offset. The offset is\n|      interpreted relative to the position indicated by whence.  Values\n|      for whence are:\n|\n|      * 0 -- start of stream (the default); offset should be zero or positive\n|      * 1 -- current stream position; offset may be negative\n|      * 2 -- end of stream; offset is usually negative\n|\n|      Return the new absolute position.\n|\n|  seekable(self)\n|      Return whether object supports random access.\n|\n|      If False, seek(), tell() and truncate() will raise OSError.\n|      This method may need to do a test seek().\n|\n|  writable(self)\n|      Return whether object was opened for writing.\n|\n|      If False, write() will raise OSError.\n|\n|  write(self, data)\n|      Write the given buffer to the IO stream.\n|\n|      Returns the number of bytes written, which is always the length of b\n|      in bytes.\n|\n|      Raises BlockingIOError if the buffer is full and the\n|      underlying raw stream cannot accept more data at the moment.\n|\n|  ----------------------------------------------------------------------\n|  Readonly properties defined here:\n|\n|  closed\n|\n|  filename\n|\n|  mtime\n|      Last modification time read from stream, or None\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  myfileobj = None\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from io.BufferedIOBase:\n|\n|  detach(self, /)\n|      Disconnect this buffer from its underlying raw stream and return it.\n|\n|      After the raw stream has been detached, the buffer is in an unusable\n|      state.\n|\n|  readinto(self, buffer, /)\n|\n|  readinto1(self, buffer, /)\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from io.IOBase:\n|\n|  del(...)\n|\n|  enter(...)\n|\n|  exit(...)\n|\n|  iter(self, /)\n|      Implement iter(self).\n|\n|  next(self, /)\n|      Implement next(self).\n|\n|  isatty(self, /)\n|      Return whether this is an 'interactive' stream.\n|\n|      Return False if it can't be determined.\n|\n|  readlines(self, hint=-1, /)\n|      Return a list of lines from the stream.\n|\n|      hint can be specified to control the number of lines read: no more\n|      lines will be read if the total size (in bytes/characters) of all\n|      lines so far exceeds hint.\n|\n|  tell(self, /)\n|      Return current stream position.\n|\n|  truncate(...)\n|      Truncate file to size bytes.\n|\n|      File pointer is left unchanged.  Size defaults to the current IO\n|      position as reported by tell().  Returns the new size.\n|\n|  writelines(self, lines, /)\n|      Write a list of lines to stream.\n|\n|      Line separators are not added, so it is usual for each of the\n|      lines provided to have a line separator at the end.\n|\n|  ----------------------------------------------------------------------\n|  Static methods inherited from io.IOBase:\n|\n|  new(*args, kwargs) from builtins.type\n|      Create and return a new object.  See help(type) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from io.IOBase:\n|\n|  dict\n"
                }
            ]
        },
        "FUNCTIONS": {
            "content": "",
            "subsections": [
                {
                    "name": "compress",
                    "content": "Compress data in one shot and return the compressed string.\nOptional argument is the compression level, in range of 0-9.\n"
                },
                {
                    "name": "decompress",
                    "content": "Decompress a gzip compressed string in one shot.\nReturn the decompressed string.\n"
                },
                {
                    "name": "open",
                    "content": "Open a gzip-compressed file in binary or text mode.\n\nThe filename argument can be an actual filename (a str or bytes object), or\nan existing file object to read from or write to.\n\nThe mode argument can be \"r\", \"rb\", \"w\", \"wb\", \"x\", \"xb\", \"a\" or \"ab\" for\nbinary mode, or \"rt\", \"wt\", \"xt\" or \"at\" for text mode. The default mode is\n\"rb\", and the default compresslevel is 9.\n\nFor binary mode, this function is equivalent to the GzipFile constructor:\nGzipFile(filename, mode, compresslevel). In this case, the encoding, errors\nand newline arguments must not be provided.\n\nFor text mode, a GzipFile object is created, and wrapped in an\nio.TextIOWrapper instance with the specified encoding, error handling\nbehavior, and line ending(s).\n"
                }
            ]
        },
        "DATA": {
            "content": "all = ['BadGzipFile', 'GzipFile', 'open', 'compress', 'decompress'...\n",
            "subsections": []
        },
        "FILE": {
            "content": "/usr/lib/python3.10/gzip.py\n\n",
            "subsections": []
        }
    },
    "summary": "gzip - Functions that read and write gzipped files.",
    "flags": [],
    "examples": [],
    "see_also": [],
    "tldr": {
        "source": "official",
        "description": "Compress/uncompress files with `gzip` compression (LZ77).",
        "examples": [
            {
                "description": "Compress a file, replacing it with a `gzip` archive",
                "command": "gzip {{path/to/file}}"
            },
            {
                "description": "Decompress a file, replacing it with the original uncompressed version",
                "command": "gzip {{-d|--decompress}} {{path/to/file.gz}}"
            },
            {
                "description": "Display the name and reduction percentage for each file compressed",
                "command": "gzip {{-v|--verbose}} {{path/to/file.gz}}"
            },
            {
                "description": "Compress a file, keeping the original file",
                "command": "gzip {{-k|--keep}} {{path/to/file}}"
            },
            {
                "description": "Compress a file, specifying the output filename",
                "command": "gzip {{-c|--stdout}} {{path/to/file}} > {{path/to/compressed_file.gz}}"
            },
            {
                "description": "Decompress a `gzip` archive specifying the output filename",
                "command": "gzip {{-cd|--stdout --decompress}} {{path/to/file.gz}} > {{path/to/uncompressed_file}}"
            },
            {
                "description": "Specify the compression level. 1 is the fastest (low compression), 9 is the slowest (high compression), 6 is the default",
                "command": "gzip -{{1..9}} {{-c|--stdout}} {{path/to/file}} > {{path/to/compressed_file.gz}}"
            },
            {
                "description": "List the contents of a compressed file",
                "command": "gzip {{-l|--list}} {{path/to/file.txt.gz}}"
            }
        ]
    }
}