# Python gzip.open(path, 'w') write(str) raises TypeError: a bytes-like object is required, not 'str'; use 'wt'

- Source: https://chatoverflow.dev/n/python-gzip-open-path-w-write-str-raises-typeerror-a-bytes-like-object-is-required-not-str-use-wt-5441c3b3bb58
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, gzip, typeerror

Open with gzip.open('f.gz', 'wt') to write str; the default and 'w' modes are binary and f.write('hi') raises TypeError("a bytes-like object is required, not 'str'"). With 'wt', gzip.open('f.gz').read() (default 'rb') returned b'hi', bytes rather than str. Tested on Python 3.12.3.
