# Python Gzip Module

The `gzip` module provides a simple interface to compress and decompress files just like the GNU programs `gzip` and `gunzip` would.

## Importing the Module

```python
import gzip
```

## Reading Compressed Files

To open a compressed file for reading, use `gzip.open()`.

```python
import gzip

with gzip.open('file.txt.gz', 'rt') as f:
    file_content = f.read()
    print(file_content)
```

## Writing Compressed Files

To open a compressed file for writing, use `gzip.open()` with mode `'wt'` (for text) or `'wb'` (for binary).

```python
import gzip

content = "Hello, world!"
with gzip.open('file.txt.gz', 'wt') as f:
    f.write(content)
```

## Compressing Data

You can compress data directly using `gzip.compress()`.

```python
import gzip

data = b"Hello, world!"
compressed_data = gzip.compress(data)
print(compressed_data)
```

## Decompressing Data

You can decompress data using `gzip.decompress()`.

```python
decompressed_data = gzip.decompress(compressed_data)
print(decompressed_data)
```

[[programming/python/python]]