Python 3: gzip.open() and modes

jeff00seattle picture jeff00seattle · Feb 2, 2017 · Viewed 11.3k times · Source

https://docs.python.org/3/library/gzip.html

I am considering to use gzip.open(), and I am a little confused about the mode argument:

The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' for binary mode, or 'rt', 'at', 'wt', or 'xt' for text mode. The default is 'rb'.

So what is the difference between 'w' and 'wb'?

The document states they are both binary mode.

So does that mean that there is no difference between 'w' and 'wb'?

Answer

Jean-François Fabre picture Jean-François Fabre · Feb 2, 2017

It means that r defaults to rb, and if you want text you have to specify it using rt.

(as opposed to open behaviour where r means rt, not rb)